Collectors toCollection() method in Java
In this post, we’ll talk about the Collectors.toCollection() method in Java.
In the previous post, we have discussed toList(), and toSet() methods of the Collectors class. Both methods return a Collector to collect the input elements into a new list or a Set, but doesn’t offer any guarantee on the type of the Collection returned. For instance, Collectors.toList() method can return an ArrayList or a LinkedList or any other implementation of the List interface.
To get the desired Collection, we can use the toCollection() method provided by the Collectors class.
1. Get the desired list type
The idea is to use the collector returned by Collectors.toCollection() method that can accept desired constructor method reference like ArrayList::new or LinkedList::new.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; // Java program to demonstrate `Collectors.toCollection()` method in Java class Main { public static void main(String[] args) { // Get an `ArrayList` List<Integer> ArrayList = Stream.of(2, 4, 6, 8, 10) .collect(Collectors.toCollection(ArrayList::new)); System.out.println("Array list is " + ArrayList); // Get a `LinkedList` List<Integer> linkedList = Stream.of(1, 3, 5, 7, 9) .collect(Collectors.toCollection(LinkedList::new)); System.out.println("Linked list is " + linkedList); } } |
Output:
Array list is [2, 4, 6, 8, 10]
Linked list is [1, 3, 5, 7, 9]
We can also use the toCollection() method to add elements of a stream to an existing list (or a set) as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; // Java program to add elements of a stream to an existing list // using `Collectors.toCollection()` method class Main { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3)); Stream<Integer> stream = Stream.of(4, 5); stream.collect(Collectors.toCollection(() -> list)); System.out.println(list); } } |
Output:
[1, 2, 3, 4, 5]
2. Get the desired set type
Creating a new set of the desired type works similarly as List. In the following program, we pass method reference of HashSet::new and TreeSet::new to get a HashSet and a TreeSet, respectively.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.HashSet; import java.util.TreeSet; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; // Java program to demonstrate `Collectors.toCollection()` method in Java class Main { public static void main(String[] args) { // get a `HashSet` Set<Integer> hashSet = Stream.of(2, 4, 6, 8, 10) .collect(Collectors.toCollection(HashSet::new)); System.out.println("Hash set is " + hashSet); // get a `TreeSet` Set<Integer> treeSet = Stream.of(1, 3, 5, 7, 9) .collect(Collectors.toCollection(TreeSet::new)); System.out.println("Tree set is " + treeSet); } } |
Output:
Hash set is [2, 4, 6, 8, 10]
Tree set is [1, 3, 5, 7, 9]
That’s all about the Collectors class toCollection() method in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)