Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:collection
  3. HashSet<E> class
HashSet
description

HashSet<E> classabstractfinal

An unordered hash-table basedSet implementation.

The elements of aHashSet must have consistent equalityand hashCode implementations. This means that the equals operationmust define a stable equivalence relation on the elements (reflexive,symmetric, transitive, and consistent over time), and that the hashCodemust be consistent with equality, so that it's the same for objects that areconsidered equal.

Most simple operations onHashSet are done in (potentially amortized)constant time:add,contains,remove, andlength, provided the hashcodes of objects are well distributed.

The iteration order of the set is not specified and depends onthe hashcodes of the provided elements. However, the order is stable:multiple iterations over the same set produce the same order, as long asthe set is not modified.

Note:Do not modify a set (add or remove elements) while an operationis being performed on that set, for example in functionscalled during aforEach orcontainsAll call,or while iterating the set.

Do not modify elements in a way which changes their equality (and thus theirhash code) while they are in the set. Some specialized kinds of sets may bemore permissive with regards to equality, in which case they should documenttheir different behavior and restrictions.

Example:

final letters = HashSet<String>();

To add data to a set, useadd oraddAll.

letters.add('A');letters.addAll({'B', 'C', 'D'});

To check if the set is empty, useisEmpty orisNotEmpty.To find the number of elements in the set, uselength.

print(letters.isEmpty); // falseprint(letters.length); // 4print(letters); // fx {A, D, C, B}

To check whether the set has an element with a specific value,usecontains.

final bExists = letters.contains('B'); // true

TheforEach method calls a function with each element of the set.

letters.forEach(print);// A// D// C// B

To make a copy of the set, usetoSet.

final anotherSet = letters.toSet();print(anotherSet); // fx {A, C, D, B}

To remove an element, useremove.

final removedValue = letters.remove('A'); // trueprint(letters); // fx {B, C, D}

To remove multiple elements at the same time, useremoveWhere orremoveAll.

letters.removeWhere((element) => element.startsWith('B'));print(letters); // fx {D, C}

To removes all elements in this set that do not meet a condition,useretainWhere.

letters.retainWhere((element) => element.contains('C'));print(letters); // {C}

To remove all elements and empty the set, useclear.

letters.clear();print(letters.isEmpty); // trueprint(letters); // {}

See also:

  • Set is the general interface of collection where each object canoccur only once.
  • LinkedHashSet objects stored based on insertion order.
  • SplayTreeSet iterates the objects in sorted order.
Implemented types
Available extensions

Constructors

HashSet({boolequals(E,E)?,inthashCode(E)?,boolisValidKey(dynamic)?})
Create a hash set using the providedequals as equality.
factory
HashSet.from(Iterableelements)
Create a hash set containing allelements.
factory
HashSet.identity()
Creates an unordered identity-based set.
factory
HashSet.of(Iterable<E>elements)
Create a hash set containing allelements.
factory

Properties

first→ E
The first element.
no setterinherited
firstOrNull→ T?

Available onIterable<T>, provided by theIterableExtensions extension

The first element of this iterator, ornull if the iterable is empty.
no setter
hashCodeint
The hash code for this object.
no setterinherited
indexedIterable<(int,T)>

Available onIterable<T>, provided by theIterableExtensions extension

Pairs of elements of the indices and elements of this iterable.
no setter
isEmptybool
Whether this collection has no elements.
no setterinherited
isNotEmptybool
Whether this collection has at least one element.
no setterinherited
iteratorIterator<E>
Provides an iterator that iterates over the elements of this set.
no setteroverride
last→ E
The last element.
no setterinherited
lastOrNull→ T?

Available onIterable<T>, provided by theIterableExtensions extension

The last element of this iterable, ornull if the iterable is empty.
no setter
lengthint
The number of elements in thisIterable.
no setterinherited
nonNullsIterable<T>

Available onIterable<T?>, provided by theNullableIterableExtensions extension

The non-null elements of this iterable.
no setter
runtimeTypeType
A representation of the runtime type of the object.
no setterinherited
single→ E
Checks that this iterable has only one element, and returns that element.
no setterinherited
singleOrNull→ T?

Available onIterable<T>, provided by theIterableExtensions extension

The single element of this iterator, ornull.
no setter
waitFuture<List<T>>

Available onIterable<Future<T>>, provided by theFutureIterable extension

Waits for futures in parallel.
no setter

Methods

add(Evalue)bool
Addsvalue to the set.
inherited
addAll(Iterable<E>elements)→ void
Adds allelements to this set.
inherited
any(booltest(Eelement))bool
Checks whether any element of this iterable satisfiestest.
inherited
asNameMap()Map<String,T>

Available onIterable<T>, provided by theEnumByName extension

Creates a map from the names of enum values to the values.
byName(Stringname)→ T

Available onIterable<T>, provided by theEnumByName extension

Finds the enum value in this list with namename.
cast<R>()Set<R>
Provides a view of this set as a set ofR instances.
inherited
clear()→ void
Removes all elements from the set.
inherited
contains(Object?value)bool
Whethervalue is in the set.
inherited
containsAll(Iterable<Object?>other)bool
Whether this set contains all the elements ofother.
inherited
difference(Set<Object?>other)Set<E>
Creates a new set with the elements of this that are not inother.
inherited
elementAt(intindex)→ E
Returns theindexth element.
inherited
elementAtOrNull(intindex)→ T?

Available onIterable<T>, provided by theIterableExtensions extension

The element at positionindex of this iterable, ornull.
every(booltest(Eelement))bool
Checks whether every element of this iterable satisfiestest.
inherited
expand<T>(Iterable<T>toElements(Eelement))Iterable<T>
Expands each element of thisIterable into zero or more elements.
inherited
firstWhere(booltest(Eelement), {EorElse()?})→ E
The first element that satisfies the given predicatetest.
inherited
fold<T>(TinitialValue,Tcombine(TpreviousValue,Eelement))→ T
Reduces a collection to a single value by iteratively combining eachelement of the collection with an existing value
inherited
followedBy(Iterable<E>other)Iterable<E>
Creates the lazy concatenation of this iterable andother.
inherited
forEach(voidaction(Eelement))→ void
Invokesaction on each element of this iterable in iteration order.
inherited
intersection(Set<Object?>other)Set<E>
Creates a new set which is the intersection between this set andother.
inherited
join([Stringseparator =""])String
Converts each element to aString and concatenates the strings.
inherited
lastWhere(booltest(Eelement), {EorElse()?})→ E
The last element that satisfies the given predicatetest.
inherited
lookup(Object?object)→ E?
If an object equal toobject is in the set, return it.
inherited
map<T>(TtoElement(Ee))Iterable<T>
The current elements of this iterable modified bytoElement.
inherited
noSuchMethod(Invocationinvocation)→ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reduce(Ecombine(Evalue,Eelement))→ E
Reduces a collection to a single value by iteratively combining elementsof the collection using the provided function.
inherited
remove(Object?value)bool
Removesvalue from the set.
inherited
removeAll(Iterable<Object?>elements)→ void
Removes each element ofelements from this set.
inherited
removeWhere(booltest(Eelement))→ void
Removes all elements of this set that satisfytest.
inherited
retainAll(Iterable<Object?>elements)→ void
Removes all elements of this set that are not elements inelements.
inherited
retainWhere(booltest(Eelement))→ void
Removes all elements of this set that fail to satisfytest.
inherited
singleWhere(booltest(Eelement), {EorElse()?})→ E
The single element that satisfiestest.
inherited
skip(intcount)Iterable<E>
Creates anIterable that provides all but the firstcount elements.
inherited
skipWhile(booltest(Evalue))Iterable<E>
Creates anIterable that skips leading elements whiletest is satisfied.
inherited
take(intcount)Iterable<E>
Creates a lazy iterable of thecount first elements of this iterable.
inherited
takeWhile(booltest(Evalue))Iterable<E>
Creates a lazy iterable of the leading elements satisfyingtest.
inherited
toList({boolgrowable =true})List<E>
Creates aList containing the elements of thisIterable.
inherited
toSet()Set<E>
Creates aSet with the same elements and behavior as thisSet.
inherited
toString()String
A string representation of this object.
inherited
union(Set<E>other)Set<E>
Creates a new set which contains all the elements of this set andother.
inherited
where(booltest(Eelement))Iterable<E>
Creates a new lazyIterable with all elements that satisfy thepredicatetest.
inherited
whereType<T>()Iterable<T>
Creates a new lazyIterable with all elements that have typeT.
inherited

Operators

operator ==(Objectother)bool
The equality operator.
inherited
  1. Dart
  2. dart:collection
  3. HashSet<E> class
dart:collection library

[8]ページ先頭

©2009-2025 Movatter.jp