The dart:collection
library provides advanced collection support for the Dart language. The library contains the HashSet<E>
class, which is an unordered hash-table-based implementation of the abstract Set<E>
class.
followedBy()
methodThe HashSet<E>
class provides followedBy()
method, which takes an Iterable as input and lazily concatenates the elements of the input to the hashset.
Note:
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 followedBy()
method works.
The syntax of the followedBy()
method is as follows:
Iterable<E> followedBy(Iterable<E> other)
other
: The followedBy()
method takes an Iterable, other
, as input and adds the other
elements after the HashSet elements in the same order.Iterable
: followedBy()
returns a lazy Iterable with the concatenation of HashSet elements and the input iterable.The code below shows how the followedBy()
method works in Dart.
import 'dart:collection';void main() {var firstMonthSet = HashSet<String>();firstMonthSet.add("January");firstMonthSet.add("February");firstMonthSet.add("March");print('Printing HashSet Elements');print(firstMonthSet);var otherMonthSet = HashSet<String>();otherMonthSet.add("April");otherMonthSet.add("May");print('Printing Other Set Elements');print(otherMonthSet);var result = firstMonthSet.followedBy(otherMonthSet);print('Concatenated Set using followedBy(): $result');}
In the above code, we see the following:
HashSet
class of type String
.add()
method to add a few month names to the HashSet: "January"
, "February"
, "March"
.followedBy()
on the HashSet passing the second HashSet as input. It returns an Iterable with five elements.print()
function of the core library to display the set elements and the result of the followedBy()
method.