HashSet in Dart and its methods and properties

Introduction to HashSet in Dart:

The HashSet class of Dart is used for a hash-table based Set implementation. The Set collection is used to hold unique elements, i.e. we can’t have any duplicates in a set. The elements of a HashSet should provide an equivalence relation and should provide an implementation of hashCode.

It is unordered. So, the iteration order might differ. The HashSet class provides a couple of properties and methods.

Constructors of a HashSet:

There are four constructors available for a HashSet.

HashSet({bool equals(E, E)?, int hashCode(E)?, bool isValidKey(dynamic)?})

This constructor is used to create one HashSet and it uses the equals method as the equality method, hashCode method for the hash-code.

HashSet.from(Iterable it)

It creates one HashSet with all the items of the iterable it.

HashSet.identity()

It creates one identity-based HashSet.

HashSet.of(Iterable it)

Creates one HashSet with all the items of the iterable it.

Properties of a HashSet:

The following properties are available for a HashSet:

isEmpty → bool

Check if the HashSet is empty.

isNotEmpty → bool

Check if the HashSet is not empty.

length → int

Get the length of the HashSet.

first → E

Get the first element.

last → E

Get the last element.

single → E

Check if it has only one element and returns that.

iterator → Iterator

Get an iterator to iterate over the HashSet.

hashCode → int

Get the hashCode.

runtimeType → Type

Get the runtime type.

Methods of HashSet:

The following methods are available for a HashSet:

remove(Object? o) → bool

It removes the object o from the HashSet.

removeAll(Iterable<Object?> it) → void

Remove all the elements of the set.

clear() → void

It removes all elements of the HashSet

removeWhere(bool fn(E e)) → void

Removes all elements as defined by the function fn.

retainAll(Iterable<Object?> it) → void

Removes all elements that are not in the iterable it.

retainWhere(bool fn(E e)) → void

Removes all elements that are failed to the function fn.

add(E e) → bool

Add the element e to the HashSet.

addAll(Iterable it) → void

Adds all the elements of the iterable it to this set.

contains(Object? o) → bool

Check if the object o is in the HashSet or not.

containsAll(Iterable<Object?> it) → bool

This method can be used to check if all the items of the iterable are in the HashSet or not.

any(bool fn(E e)) → bool

Find if any element in the set satisfies the function fn.

every(bool fn(E e)) → bool

Find if all elements of the set satisfy the function fn.

elementAt(int i) → E

Return the element at index i of the HashSet.

firstWhere(bool fn(E element), {E orElse()?}) → E

Get the first element that satisfies the fn.

singleWhere(bool fn(E element), {E orElse()?}) → E

Get the single element that satisfies the fn.

lastWhere(bool fn(E element), {E orElse()?}) → E

Get the last element that satisfies the fn.

lookup(Object? o) → E?

Get the element equal to o in the set.

difference(Set<Object?> o) → Set

Get a new set with the elements that are in the current set but not in o.

intersection(Set<Object?> o) → Set

Get the intersection between the current set and o.

union(Set o) → Set

Get the union of the elements of the current set and the o.

skip(int count) → Iterable

Get one iterable by skipping the first count elements of the set.

skipWhile(bool fn(E e)) → Iterable

Get one iterable by skipping all the elements defined by the function fn.

forEach(void fn(E e)) → void

Call the function fn on each of the elements of the set.

map(T toElement(E e)) → Iterable

Modify the elements by the toElement function.

take(int count) → Iterable

Get one lazy iterable of the first count elements of the set.

takeWhile(bool fn(E e)) → Iterable

Get one lazy iterable of the first elements defined by the function fn.

where(bool fn(E e)) → Iterable

Get one lazy iterable of the elements satisfied by the predicate fn.

whereType() → Iterable

Get one lazy iterable of the elements of type T.

followedBy(Iterable it) → Iterable

Get one lazy iterable of this set and it.

reduce(E combine(E v, E e)) → E

Reduce the set to a single value by combining iteratively with the combine function.

fold(T initial, T combine(T previous, E element)) → T

It is similar to the reduce function, but it uses an initial value with the combine function.

expand(Iterable toElements(E e)) → Iterable

Expand each of the items of the set to one or more elements.

join([String separator = ""]) → String

Returns one string by concatenating the elements of the HashSet. It converts the elements to string.

toList({bool growable = true}) → List

It returns one list with the set elements.

toSet() → Set

Get one set with the elements of the HashSet.

toString() → String

Get the string representation of the HashSet.

cast() → Set

Get a set of R instance.

noSuchMethod(Invocation invocation) → dynamic

This is invoked if any undefined method is called.

You might also like: