Friday, September 5, 2025
HomeLanguagesJavaCollectors toSet() in Java with Examples

Collectors toSet() in Java with Examples

Collectors toSet() returns a Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned. This is an unordered Collector i.e, the collection operation does not commit to preserving the encounter order of input elements.

Syntax:

public static <T> Collector<T, ?, Set<T>> toSet()

where:

  • T: The type of the input elements.
  • Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.
    • T: The type of input elements to the reduction operation.
    • A: The mutable accumulation type of the reduction operation.
    • R: The result type of the reduction operation.
  • Set: A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.

Return Value: A Collector which collects all the input elements into a Set.

Below are the examples to illustrate toSet() method:

Example 1:




// Java code to show the implementation of
// Collectors toSet() function
  
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a Stream of strings
        Stream<String> s = Stream.of("Geeks",
                                     "for",
                                     "Lazyroar",
                                     "Geeks Classes");
  
        // using Collectors toSet() function
        Set<String> mySet = s.collect(Collectors.toSet());
  
        // printing the elements
        System.out.println(mySet);
    }
}


Output:

[Geeks Classes, Lazyroar, Geeks, for]

Example 2:




// Java code to show the implementation of
// Collectors toSet() function
  
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // creating a Stream of strings
        Stream<String> s = Stream.of("1", "2", "3", "4");
  
        // using Collectors toSet() function
        Set<String> mySet = s.collect(Collectors.toSet());
  
        // printing the elements
        System.out.println(mySet);
    }
}


Output:

[1, 2, 3, 4]
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6637 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS