A Java 8 example will show you how to convert a Stream to a List through collectors.tolist. Java8example1.java
Package com.mkyong.java8;
Import java.util.List;
Import java.util.stream.Collectors;
Import Java.util.stream.Stream;
public class Java8example1 {public
static void Main (string[] args) {
stream<string> language = Stream.of (" Java "," Python "," Node ");
Convert a Stream to List
list<string> result = Language.collect (Collectors.tolist ());
Result.foreach (System.out::p rintln);
}
Output
Java
python
node
Another example is to filter the number 3 and convert it to list. Java8example2.java
Package com.mkyong.java8;
Import java.util.List;
Import java.util.stream.Collectors;
Import Java.util.stream.Stream;
public class Java8example2 {public
static void Main (string[] args) {
stream<integer> number = Stream.of (1, 2, 3, 4, 5);
list<integer> result2 = number.filter (x-> x!= 3). Collect (Collectors.tolist ());
Result2.foreach (x-> System.out.println (x));
}
Output
1
2
4
5
ReferencesJava 8 Streams Filter Examples Java 8 collectors JavaDoc