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.
addAll()
methodThe 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
, andcontains
operations.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.
The syntax of the addAll()
method is as follows:
void addAll(Iterable<E> elements)
The addAll()
method takes elements as input and adds them to the HashSet.
This method does not return a value.
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');}
We create an instance of a HashSet
class of type String
.
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. The addAll()
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 the addAll()
method.