Collectors toSet() in Java with Examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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", "GeeksforGeeks", "Geeks Classes"); // using Collectors toSet() function Set<String> mySet = s.collect(Collectors.toSet()); // printing the elements System.out.println(mySet); } } Output: [Geeks Classes, GeeksforGeeks, Geeks, for] Example 2: Java // 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] Comment More infoAdvertise with us Next Article Collectors toSet() in Java with Examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream Java-Stream-Collectors Java-Collectors +3 More Practice Tags : JavaMisc Similar Reads Collectors toList() method in Java with Examples The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method c 2 min read Collection add() Method in Java with Examples The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet 4 min read Collections newSetFromMap() method in Java with Examples The newSetFromMap() method of java.util.Collections class is used to return a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides a Set implementation corresponding to 2 min read Collectors groupingBy() method in Java with Examples The groupingBy() method of Collectors class in Java are used for grouping objects by some property and storing results in a Map instance. In order to use it, we always need to specify a property by which the grouping would be performed. This method provides similar functionality to SQL's GROUP BY cl 2 min read Collection clear() method in Java with Examples The collection clear() method of Java Collection Interface clears the Collection upon which it is called. After this method is called, the collection will be empty as it removes all the elements from the collection. This method does not take any parameter and does not return any value. Example: Java 3 min read Like