Collectors toSet() in Java with Examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes 3 Likes 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] Create Quiz Comment S Sahil_Bansall Follow 3 Improve S Sahil_Bansall Follow 3 Improve Article Tags : Misc Java Java - util package Java-Functions java-stream Java-Stream-Collectors Java-Collectors +3 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like