HashSet<E> class Null safety

An unordered hash-table based Set implementation.

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

Most simple operations on HashSet are done in (potentially amortized) constant time: add, contains, remove, and length, provided the hash codes of objects are well distributed.

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

Note: Do not modify a set (add or remove elements) while an operation is being performed on that set, for example in functions called during a forEach or containsAll call, or while iterating the set.

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

Example:

final letters = HashSet<String>();

To add data to a set, use add or addAll.

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

To check if the set is empty, use isEmpty or isNotEmpty. To find the number of elements in the set, use length.

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

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

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

The forEach method calls a function with each element of the set.

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

To make a copy of the set, use toSet.

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

To remove an element, use remove.

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

To remove multiple elements at the same time, use removeWhere or removeAll.

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

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

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

To remove all elements and empty the set, use clear.

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

See also:

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

Constructors

HashSet({bool equals(E, E)?, int hashCode(E)?, bool isValidKey(dynamic)?})
Create a hash set using the provided equals as equality.
factory
HashSet.from(Iterable elements)
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
Returns the first element.
read-only, inherited
hashCode int
The hash code for this object.
read-only, inherited
isEmpty bool
Whether this collection has no elements.
read-only, inherited
isNotEmpty bool
Whether this collection has at least one element.
read-only, inherited
iterator Iterator<E>
Provides an iterator that iterates over the elements of this set.
read-only, override
last → E
Returns the last element.
read-only, inherited
length int
Returns the number of elements in the iterable.
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
single → E
Checks that this iterable has only one element, and returns that element.
read-only, inherited

Methods

add(E value) bool
Adds value to the set.
inherited
addAll(Iterable<E> elements) → void
Adds all elements to this set.
inherited
any(bool test(E element)) bool
Checks whether any element of this iterable satisfies test.
inherited
cast<R>() Set<R>
Provides a view of this set as a set of R instances.
inherited
clear() → void
Removes all elements from the set.
inherited
contains(Object? value) bool
Whether value is 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(int index) → E
Returns the indexth element.
inherited
every(bool test(E element)) bool
Checks whether every element of this iterable satisfies test.
inherited
expand<T>(Iterable<T> toElements(E element)) Iterable<T>
Expands each element of this Iterable into zero or more elements.
inherited
firstWhere(bool test(E element), {E orElse()?}) → E
Returns the first element that satisfies the given predicate test.
inherited
fold<T>(T initialValue, T combine(T previousValue, E element)) → T
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value
inherited
followedBy(Iterable<E> other) Iterable<E>
Returns the lazy concatenation of this iterable and other.
inherited
forEach(void action(E element)) → void
Invokes action 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 and other.
inherited
join([String separator = ""]) String
Converts each element to a String and concatenates the strings.
inherited
lastWhere(bool test(E element), {E orElse()?}) → E
Returns the last element that satisfies the given predicate test.
inherited
lookup(Object? object) → E?
If an object equal to object is in the set, return it.
inherited
map<T>(T toElement(E e)) Iterable<T>
The current elements of this iterable modified by toElement.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
reduce(E combine(E value, E element)) → E
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
inherited
remove(Object? value) bool
Removes value from the set.
inherited
removeAll(Iterable<Object?> elements) → void
Removes each element of elements from this set.
inherited
removeWhere(bool test(E element)) → 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(bool test(E element)) → void
Removes all elements of this set that fail to satisfy test.
inherited
singleWhere(bool test(E element), {E orElse()?}) → E
Returns the single element that satisfies test.
inherited
skip(int count) Iterable<E>
Returns an Iterable that provides all but the first count elements.
inherited
skipWhile(bool test(E value)) Iterable<E>
Returns an Iterable that skips leading elements while test is satisfied.
inherited
take(int count) Iterable<E>
Returns a lazy iterable of the count first elements of this iterable.
inherited
takeWhile(bool test(E value)) Iterable<E>
Returns a lazy iterable of the leading elements satisfying test.
inherited
toList({bool growable = true}) List<E>
Creates a List containing the elements of this Iterable.
inherited
toSet() Set<E>
Creates a Set 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(bool test(E element)) Iterable<E>
Returns a new lazy Iterable with all elements that satisfy the predicate test.
inherited
whereType<T>() Iterable<T>
Returns a new lazy Iterable with all elements that have type T.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited