Java Stream | Collectors toCollection() in Java
Collectors toCollection(Supplier<C> collectionFactory) method in Java is used to create a Collection using Collector. It returns a Collector that accumulates the input elements into a new Collection, in the order in which they are passed.
Syntax:
public static <T, C extends Collection<T>> Collector<T, ?, C>
toCollection(Supplier<C> collectionFactory)
where:-
- Collection : The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.
- 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.
- Supplier : A functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
- collectionFactory : A Supplier which returns a new, empty Collection of the appropriate type.
Parameters: This method takes a mandatory parameter collectionFactory of type Supplier which returns a new, empty Collection of the appropriate type.
Return Value: This method returns a collector which collects all the input elements into a Collection, in encounter order.
Below given are some examples to illustrate the implementation of toCollection() in a better way:
Example 1:
// Java code to demonstrate Collectors // toCollection(Supplier collectionFactory) method import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a string stream Stream<String> s = Stream.of("Geeks", "for", "GeeksClasses"); // Using toCollection() method // to create a collection Collection<String> names = s .collect(Collectors .toCollection(TreeSet::new)); // Printing the elements System.out.println(names); } } |
[Geeks, GeeksClasses, for]
Example 2:
// Java code to demonstrate Collectors // toCollection(Supplier collectionFactory) method import java.util.Collection; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a string stream Stream<String> s = Stream.of("2.5", "6", "4"); // Using Collectors toCollection() Collection<String> names = s .collect(Collectors .toCollection(TreeSet::new)); // Printing the elements System.out.println(names); } } |
[2.5, 4, 6]
Recommended Posts:
- Difference between Stream.of() and Arrays.stream() method in Java
- Java | Collectors averagingDouble() with Examples
- Java 8 | Collectors counting() with Examples
- Collectors partitioningBy() method in Java
- Java 8 | Collectors averagingInt() with Examples
- Collectors toSet() in Java with Examples
- Collectors collectingAndThen() method in Java with Examples
- Collectors toList() method in Java with Examples
- Collectors toMap() method in Java with Examples
- Collectors groupingBy() method in Java with Examples
- Character Stream Vs Byte Stream in Java
- Java | Collectors averagingLong (ToLongFunction mapper) with Examples
- Java 8 Streams | Collectors.joining() method with Examples
- Java | Collectors minBy(Comparator comparator) with Examples
- Java | Collectors maxBy(Comparator comparator) 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 contribute@geeksforgeeks.org. 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.


