Table of Contents
In this post, we will see how to convert Stream to List in java.
There are multiple ways to convert Stream to List in java.
Using Collectors.toList()
You can pass Collectors.toList()
to Stream.collect()
method to convert Stream to List in java. Stream’s collect method performs mutable reduction operation on elements of Stream and Collectors.toList()
provides a collector which accumulates elements of Stream into the list.
Here is an quick example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ConvertStreamToListMain { public static void main(String args[]) { Stream<String> countriesStream = Stream.of("India", "China", "France", "Germany"); List<String> listOfCountiesName = countriesStream.collect(Collectors.toList()); System.out.println(listOfCountiesName); } } |
Output:
Using Collectors.toCollection()
You can pass Collectors.toCollection()
to Stream.collect()
method to convert Stream to List in java. This is similar to previous approach, but it gives you more control over type of collection you want.
Here is an example to create LinkedList from Stream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ConvertStreamToListMain { public static void main(String args[]) { Stream<String> countriesStream = Stream.of("India", "China", "France", "Germany"); List<String> listOfCountiesName = countriesStream.collect(Collectors.toCollection(LinkedList::new)); System.out.println(listOfCountiesName); } } |
Using foreach
You can iterate over Stream using foreach()
method and add elements to the List.
Here is an example to create LinkedList from Stream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; public class ConvertStreamToListMain { public static void main(String args[]) { Stream<String> countriesStream = Stream.of("India", "China", "France", "Germany"); List<String> listOfCountiesName=new ArrayList<>(); countriesStream.forEach(listOfCountiesName::add); System.out.println(listOfCountiesName); } } |
This approach is not recommended in case you are using Parallel Stream as elements of the Streams may not be processed in original order. You can use forEachOrdered()
method to preserve the order in the List.
Output:
Filter Stream and convert to List
If you want to filter elements from Stream, you can use Stream’s filter method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ConvertStreamToListMain { public static void main(String args[]) { Stream<String> countriesStream = Stream.of("India", "China", "France", "Germany","Indonesia"); List<String> listOfCountiesName = countriesStream.filter(country ->country.startsWith("I")) .collect(Collectors.toList()); System.out.println(listOfCountiesName); } } |
Output:
Filter method accepts predicate functional interface to filter a Stream.
Convert infinite Stream to List
You need to limit infinite Stream and then convert it to list.
Here is an exmple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class ConvertStreamToListMain { public static void main(String args[]) { IntStream infiniteIntStream = IntStream.iterate(100, i -> i+1); List<Integer> intList = infiniteIntStream.limit(5) .boxed() .collect(Collectors.toList()); System.out.println(intList); } } |
Output:
Here we have used iterate()
method to generate an infinite Stream and limited it using limit()
method and then converted it to list.
That’s all about Java Stream to List.