Java8 将数据流转换为列表

mac2022-06-30  52

引言

示例演示如何通过 Collectors.toList 将数据流转换为 List。

这个在前面也多次出现过 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 python node 过滤数字 3 并将其转换为 List 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);

输出:

1 2 4 5

源码见:java-8-demo

系列文章详见:Java 8 教程

转载于:https://www.cnblogs.com/itzjm/articles/11426620.html

最新回复(0)