toHashSet method

HashSet<T> toHashSet()

Creates a new HashSet containing all the elements of this iterable.

This method provides an easy way to convert an iterable into a HashSet. A HashSet is a collection of unique items which does not preserve the order of elements.

  • Returns: A HashSet containing all the unique elements of this iterable.

  • Examples:

    List<int> numbers = [1, 2, 3, 2, 1];
    HashSet<int> uniqueNumbers = numbers.toHashSet();
    print(uniqueNumbers); // HashSet containing 1, 2, 3
    

Note: The HashSet will only contain unique elements. If the iterable has duplicates, they will be removed in the resulting HashSet.

Implementation

HashSet<T> toHashSet() => HashSet.from(this);