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.
public static <T> Collector<T, ?, Set<T>> toSet()
- 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:
[Geeks Classes, GeeksforGeeks, Geeks, for]
[1, 2, 3, 4]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java 8 | Collectors counting() with Examples
- Java 8 Streams | Collectors.joining() method with Examples
- Java | Collectors averagingDouble() with Examples
- Java | Collectors averagingLong (ToLongFunction mapper) with Examples
- Java 8 | Collectors averagingInt() with Examples
- Java | Collectors maxBy(Comparator comparator) with Examples
- Java | Collectors minBy(Comparator comparator) with Examples
- Collectors toList() method in Java with Examples
- Collectors toMap() method in Java with Examples
- Collectors collectingAndThen() method in Java with Examples
- Collectors groupingBy() method in Java with Examples
- Java Stream | Collectors toCollection() in Java
- Collectors partitioningBy() method in Java
- Types of JVM Garbage Collectors in Java with implementation details
- Java Collectors
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.BitSet class in Java with Examples | Set 1
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.frequency() in Java with Examples
- Java.util.Arrays.equals() in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.