Convert a List to a Stream in Java
This post will discuss how to convert a list to stream in Java 8 and above. We will also discuss how to apply filters on a stream and convert it back to a list.
1. Convert a list to Stream
Converting a list to stream is very simple. As List extends the Collection interface, we can use the Collection.stream() method that returns a sequential stream of elements in the list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; class Main { // Generic method to convert a list to stream private static <T> Stream<T> listToStream (List<T> list) { return list.stream(); } // Program to convert a list to stream in Java 8 and above public static void main(String[] args) { List<String> cities = Arrays.asList("New York","Tokyo","New Delhi"); Stream<String> stream = listToStream(cities); System.out.println(Arrays.toString(stream.toArray())); } } |
Output:
[New York, Tokyo, New Delhi]
2. Filter stream using a Predicate
For filtering a stream, we can use the filter(Predicate) method that returns a stream consisting of elements matching the specified predicate.
In the following example, we’ll create a stream of string objects using Collection.stream() and filter it to produce a stream containing strings starting with “N”.
|
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 |
import java.util.Arrays; import java.util.List; import java.util.function.Predicate; import java.util.stream.Stream; class Main { // Program to convert a list to stream and filter it in Java 8 and above public static void main(String[] args) { List<String> cities = Arrays.asList("New York", "Tokyo", "New Delhi"); Predicate<String> predicate = new Predicate<String>() { @Override public boolean test(String s) { // filter cities that start with `N` return s.startsWith("N"); } }; cities.stream() .filter(predicate) .forEach(System.out::println); } } |
Output:
New York
New Delhi
We can also specify a lambda expression or method reference in place of the predicate:
|
1 2 3 |
list.stream() .filter(s -> s.startsWith("N")) .forEach(System.out::println); |
3. Convert stream back to list
We can accumulate the stream elements into a new list using a Collector returned by Collectors.toList().
|
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 32 33 34 35 |
import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { // Generic method to convert a list to stream private static <T> Stream<T> listToStream (List<T> list) { return list.stream(); } // Generic method to convert a stream to a list private static <T> List<T> streamToList (Stream<T> stream) { return stream .filter(Objects::nonNull) // optionally apply any filter .collect(Collectors.toList()); } // Program to convert the stream to list in Java 8 and above public static void main(String[] args) { List<String> cities = Arrays.asList("New York","Tokyo","New Delhi"); // convert a list to stream Stream<String> stream = listToStream(cities); // convert stream back to list cities = streamToList(stream); System.out.println(cities); } } |
Output:
[1, 2, 3, 4, 5]
We can also accumulate the stream elements into a set using Collectors.toSet(), as shown below:
|
1 2 3 4 |
// Generic method to convert a stream to a set private static <T> Set<T> streamToSet (Stream<T> stream) { return stream.collect(Collectors.toSet()); } |
That’s all about converting a List to Stream 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 :)