What is HashSet.addAll() in Dart?
The dart:collection library provides advanced collection support for the Dart language. The library contains the HashSet<E> class, an unordered hash-table-based implementation of the abstract Set<E> class.
The addAll() method
The HashSet<E> class contains the addAll() method, which adds all elements of the specified input collection to the HashSet.
HashSet<E>is a collection of unique elements that provides constant-timeadd,remove, andcontainsoperations.HashSet<E>does not have a specified iteration order. However, multiple iterations on the set produce the same element order.
The figure below illustrates how the addAll() method works.
Syntax
The syntax of the addAll() method is as follows:
void addAll(Iterable<E> elements)
Parameters
The addAll() method takes elements as input and adds them to the HashSet.
Return value
This method does not return a value.
Code
The code below shows how the addAll() method works in Dart.
import 'dart:collection';void main() {var monthSet = HashSet<String>();monthSet.add("January");print('Hash Set Elements : $monthSet');List<String> nextMonths = ["February", "March","April", "May"];print('Input List Elements : $nextMonths');var result = monthSet.addAll(nextMonths);print('Hash Set Elements : $monthSet');}
Explanation
-
We create an instance of a
HashSetclass of typeString. -
Next, we use the
add()method and add"January"to the HashSet. -
We create a list of strings and add
"February","March","April", and"May"to it. -
We call the
addAll()method and pass the previous list as input. TheaddAll()method adds all elements of the list to the HashSet. -
We use the
print()function of the core library to display the set elements and the result of theaddAll()method.