示例演示如何经过 Collectors.toList 将数据流转换为 List。html
public static void main(String[] args) { Stream<String> language = Stream.of("java", "python", "node"); //Convert a Stream to List List<String> list = language.collect(Collectors.toList()); list.forEach(System.out::println); }
输出:java
java python node
Stream<Integer> number = Stream.of(1, 2, 3, 4, 5); List<Integer> collect = number.filter(x -> x != 3).collect(Collectors.toList()); collect.forEach(System.out::println);
输出:node
1 2 4 5
源码见: java-8-demo
系列文章详见:Java 8 教程python