What is the HashSet.followedBy() method in Dart?
Overview
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.
The followedBy() method
The 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, 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 followedBy() method works.
Syntax
The syntax of the followedBy() method is as follows:
Iterable<E> followedBy(Iterable<E> other)
Parameters
other: ThefollowedBy()method takes an Iterable,other, as input and adds theotherelements after the HashSet elements in the same order.
Return value
Iterable:followedBy()returns a lazy Iterable with the concatenation of HashSet elements and the input iterable.
Code
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');}
Code explanation
In the above code, we see the following:
- Line 3: We create an instance of a
HashSetclass of typeString. - Lines 4–6: Next, we use the
add()method to add a few month names to the HashSet:"January","February","March". - Lines 10–12: We create another HashSet and add the names of two months to it.
- Line 16: We call the
followedBy()on the HashSet passing the second HashSet as input. It returns an Iterable with five elements. - Line 17: We use the
print()function of the core library to display the set elements and the result of thefollowedBy()method.