In this Convert Stream to List in Java post, we show you various ways to convert a stream to a list in Java 8. This is one of the common problem while working with Stream API. E.g. when you are processing a List using Stream’s map and filter method, you want to get the result in some collection for sure to do other processing.
Here are 5 simple ways to convert Stream to List in Java. We introduce to convert a Stream of String to a List of String, then you can do converting a Stream of Integer to List of Integer and so on by yourself.
1. Using Collectors.toList() method
This is the normal way to collect the result of stream in a container in List and you can collect to Set or any Collection as well.
1 2 |
// 1st way, using Collectors.toList() method List<String> resultList = streamExample.collect(Collectors.toList()); |
2. Using Collectors.toCollection() method
We can collect elements of Stream in any Collection, including ArrayList, LinkedList and other List as well. In this example, we are collecting Stream elements into ArrayList via interface method ArrayList::new.
1 2 |
// 2nd way, using Collectors.toCollection method resultList = streamExample.collect(Collectors.toCollection(ArrayList::new)); |
3. Using forEach() method
Going through all element of Stream one by one and add them into a new List or ArrayList.
1 2 3 |
// 3rd way, using add method List<String> list = new ArrayList<>(); streamExample.forEach(list::add); |
4. Using forEachOrdered method
Here we are using Parallel stream and this is extension of 3rd way. I noticed that elements of parallel stream be processed out of order. So we want to keep order of elements in Stream we must use forEachOrdered() method.
1 2 3 |
// 4th way, to convert Parallel Stream to List using add method List<String> list2 = new ArrayList<>(); streamExample.parallel().forEachOrdered(list2::add); |
5. Using toArray() method
Stream supports to convert Stream to array via toArray() method:
1 |
List<String> list3 = Arrays.asList(streamExample.toArray(String[]::new)); |
Demonstrate 5 ways to convert stream to list in Java
We are creating main method like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public static void main(String args[]) throws IOException { Stream<String> streamExample = Stream.of("java", "nodejs", "Java", "php", "C#", "c#", "JAVA"); // 1st way, using Collectors.toList() method List<String> resultList = streamExample.collect(Collectors.toList()); System.out.println("1st example : " + resultList); // 2nd way, using Collectors.toCollection method streamExample = Stream.of("java", "nodejs", "Java", "php", "C#", "c#", "JAVA"); resultList = streamExample.collect(Collectors.toCollection(ArrayList::new)); System.out.println("2nd Way : " + resultList); // 3rd way, using add method streamExample = Stream.of("java", "nodejs", "Java", "php", "C#", "c#", "JAVA"); List<String> list = new ArrayList<>(); streamExample.forEach(list::add); System.out.println("3rd Way : " + list); // 4th way, to convert Parallel Stream to List using add method streamExample = Stream.of("java", "nodejs", "Java", "php", "C#", "c#", "JAVA"); List<String> list2 = new ArrayList<>(); streamExample.parallel().forEachOrdered(list2::add); System.out.println("4th Way : " + list2); // 5th way of creating List from Stream in Java streamExample = Stream.of("java", "nodejs", "Java", "php", "C#", "c#", "JAVA"); List<String> list3 = Arrays.asList(streamExample.toArray(String[]::new)); System.out.println("5th example : " + list3); } |
Output
That’s all about Convert Stream to List in Java.
References
Java 8 Collectors JavaDoc
Download complete source, click link below
Java8StreamToList.zip (210 downloads)