In this tutorial, we will see “How to convert a Stream to a List in Java 8?”
To achieve that, we are going to use Collectors.toList() operation available since Java 8.
Convert a Stream to a List in Java 8 with example...!!! Share on X
Example 1. With String
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 36 37 38 | import java.util.List; import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; public class Convert_Stream_To_List_Java_8_Example_1 { public static void main(String[] args) { System.out.println("Example 1: (Basic example with String) - Convert Stream to List\n"); Stream <String> persons = Stream.of("Tom", "John", "Peterson", "Tony", "Tim"); //Convert Stream to List List <String> personList = persons.collect(Collectors.toList()); //Below mentioned all three forms will generate the same result. You can use any of those options. //Printing list using Method Reference System.out.println("Displaying list using Method Reference\n"); personList.forEach(System.out::println); //Printing list using Lambda Expression System.out.println("\nDisplaying list using Lambda Expression\n"); personList.forEach(person -> System.out.println(person)); //Printing list using Annonymous Inner Class System.out.println("\nDisplaying list using Annonymous Inner Class"); personList.forEach(new Consumer <String> () { @Override public void accept(String person) { System.out.println(person); } }); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Example 1: (Basic example with String) - Convert Stream to List Displaying list using Method Reference Tom John Peterson Tony Tim Displaying list using Lambda Expression Tom John Peterson Tony Tim Displaying list using Annonymous Inner Class Tom John Peterson Tony Tim |
Example 2. With Filtered String
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.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Convert_Stream_To_List_Java_8_Example_2 { public static void main(String[] args) { System.out.println("Example 2: (with String but filtered) - Convert Stream to List\n"); Stream <String> persons = Stream.of("Tom", "John", "Peterson", "Tony", "Tim"); //Find person whose name starts with 'T' and then convert the Stream to List List <String> personList = persons .filter(person -> person.startsWith("T")) .collect(Collectors.toList()); System.out.println("Displaying list using Method Reference\n"); personList.forEach(System.out::println); } } |
1 2 3 4 5 6 7 | Example 2: (with String but filtered) - Convert Stream to List Displaying list using Method Reference Tom Tony Tim |
Example 3. With Integer
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 36 37 38 | import java.util.List; import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; public class Convert_Stream_To_List_Java_8_Example_3 { public static void main(String[] args) { System.out.println("Example 3: (Basic example with Integer) - Convert Stream to List\n"); Stream <Integer> numbers = Stream.of(10, 90, 43, 81, 25); //Convert Stream to List List <Integer> numberList = numbers.collect(Collectors.toList()); //Below mentioned all three forms will generate the same result. You can use any of those options. //Printing list using Method Reference System.out.println("Displaying list using Method Reference\n"); numberList.forEach(System.out::println); //Printing list using Lambda Expression System.out.println("\nDisplaying list using Lambda Expression\n"); numberList.forEach(person -> System.out.println(person)); //Printing list using Annonymous Inner Class System.out.println("\nDisplaying list using Annonymous Inner Class"); numberList.forEach(new Consumer <Integer> () { @Override public void accept(Integer number) { System.out.println(number); } }); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Example 3: (Basic example with Integer) - Convert Stream to List Displaying list using Method Reference 10 90 43 81 25 Displaying list using Lambda Expression 10 90 43 81 25 Displaying list using Annonymous Inner Class 10 90 43 81 25 |
Example 4. With Filtered Integer
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.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Convert_Stream_To_List_Java_8_Example_4 { public static void main(String[] args) { System.out.println("Example 4: (with Integer but filtered) - Convert Stream to List\n"); Stream <Integer> numbers = Stream.of(10, 90, 43, 81, 25); //Find even numbers from the stream and then convert the Stream to List List < Integer > numberList = numbers .filter(number -> number % 2 == 0) .collect(Collectors.toList()); System.out.println("Displaying list using Method Reference\n"); numberList.forEach(System.out::println); } } |
1 2 3 4 5 6 | Example 4: (with Integer but filtered) - Convert Stream to List Displaying list using Method Reference 10 90 |
Do you like this Post? – then check my other helpful posts:
- Double the even / odd numbers of a specified ArrayList using Streams
- Double the numbers of specified ArrayList using Streams