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'); // trueTheforEach method calls a function with each element of the set.
letters.forEach(print);// A// D// C// BTo 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
- Set<
E>
- Set<
- Available extensions
Constructors
- HashSet({boolequals(E,E)?,inthashCode(E)?,boolisValidKey(dynamic)?})
- Create a hash set using the provided
equalsas equality.factory - HashSet.from(Iterableelements)
- Create a hash set containing all
elements.factory - HashSet.identity()
- Creates an unordered identity-based set.factory
- HashSet.of(Iterable<
E> elements) - Create a hash set containing all
elements.factory
Properties
- first→ E
- The first element.no setterinherited
- firstOrNull→ T?
Available onIterable<
The first element of this iterator, orT> , provided by theIterableExtensions extensionnullif the iterable is empty.no setter- hashCode→int
- The hash code for this object.no setterinherited
- indexed→Iterable<
(int,T)> Available onIterable<
Pairs of elements of the indices and elements of this iterable.T> , provided by theIterableExtensions extensionno setter- isEmpty→bool
- Whether this collection has no elements.no setterinherited
- isNotEmpty→bool
- Whether this collection has at least one element.no setterinherited
- iterator→Iterator<
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<
The last element of this iterable, orT> , provided by theIterableExtensions extensionnullif the iterable is empty.no setter- length→int
- The number of elements in thisIterable.no setterinherited
- nonNulls→Iterable<
T> Available onIterable<
The non-T?> , provided by theNullableIterableExtensions extensionnullelements of this iterable.no setter- runtimeType→Type
- 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<
The single element of this iterator, orT> , provided by theIterableExtensions extensionnull.no setter- wait→Future<
List< T> > Available onIterable<
Waits for futures in parallel.Future< , provided by theFutureIterable extensionT> >no setter
Methods
- add(
Evalue)→bool - Adds
valueto the set.inherited - addAll(
Iterable< E> elements)→ void - Adds all
elementsto this set.inherited - any(
booltest(Eelement))→bool - Checks whether any element of this iterable satisfies
test.inherited - asNameMap(
)→Map< String,T> Available onIterable<
Creates a map from the names of enum values to the values.T> , provided by theEnumByName extension- byName(
Stringname)→ T Available onIterable<
Finds the enum value in this list with nameT> , provided by theEnumByName extensionname.- cast<
R> ()→Set< R> - Provides a view of this set as a set of
Rinstances.inherited - clear(
)→ void - Removes all elements from the set.inherited
- contains(
Object?value)→bool - Whether
valueis in the set.inherited - containsAll(
Iterable< Object?> other)→bool - Whether this set contains all the elements of
other.inherited - difference(
Set< Object?> other)→Set<E> - Creates a new set with the elements of this that are not in
other.inherited - elementAt(
intindex)→ E - Returns the
indexth element.inherited - elementAtOrNull(
intindex)→ T? Available onIterable<
The element at positionT> , provided by theIterableExtensions extensionindexof this iterable, ornull.- every(
booltest(Eelement))→bool - Checks whether every element of this iterable satisfies
test.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 predicate
test.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 valueinherited
- followedBy(
Iterable< E> other)→Iterable<E> - Creates the lazy concatenation of this iterable and
other.inherited - forEach(
voidaction(Eelement))→ void - Invokes
actionon 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 and
other.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 predicate
test.inherited - lookup(
Object?object)→ E? - If an object equal to
objectis in the set, return it.inherited - map<
T> (TtoElement(Ee))→Iterable< T> - The current elements of this iterable modified by
toElement.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 - Removes
valuefrom the set.inherited - removeAll(
Iterable< Object?> elements)→ void - Removes each element of
elementsfrom this set.inherited - removeWhere(
booltest(Eelement))→ void - Removes all elements of this set that satisfy
test.inherited - retainAll(
Iterable< Object?> elements)→ void - Removes all elements of this set that are not elements in
elements.inherited - retainWhere(
booltest(Eelement))→ void - Removes all elements of this set that fail to satisfy
test.inherited - singleWhere(
booltest(Eelement), {EorElse()?})→ E - The single element that satisfies
test.inherited - skip(
intcount)→Iterable< E> - Creates anIterable that provides all but the first
countelements.inherited - skipWhile(
booltest(Evalue))→Iterable< E> - Creates an
Iterablethat skips leading elements whiletestis satisfied.inherited - take(
intcount)→Iterable< E> - Creates a lazy iterable of the
countfirst elements of this iterable.inherited - takeWhile(
booltest(Evalue))→Iterable< E> - Creates a lazy iterable of the leading elements satisfying
test.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 this
Set.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 and
other.inherited - where(
booltest(Eelement))→Iterable< E> - Creates a new lazyIterable with all elements that satisfy thepredicate
test.inherited - whereType<
T> ()→Iterable< T> - Creates a new lazyIterable with all elements that have type
T.inherited
Operators
- operator ==(
Objectother)→bool - The equality operator.inherited