Java 8 Stream流笔记
date
Aug 31, 2021
slug
Stream
status
Published
tags
Stream
summary
Stream流笔记
type
Post
Language
Java 8 Stream流笔记
自己的理解:在 Java 对数据的操作可以用类似 SQL 的查询语句一样进行聚合和计算
生成流
创建流
stream() - 为集合创建流list.stream()创建并行流
parallelStream() - 为集合创建并行流list.parallelStream()转换
将流转换成集合
转换成List
collect(Collectors.toList()) - 将流转换成Listlist = list.stream().collect(Collectors.toList());转换成Map
collect(Collectors.toMap(K,V)) - 将流转换成Map方法
filter - 过滤
方法内为 lambda 表达式
List<Integer> list = Arrays.asList(1,2,3,3,9,8);
// 过滤大于1的元素
list = list.stream().filter(v -> v > 1).collect(Collectors.toList());
// 2,3,3,9,8distinct - 去重
该方法是通过
equals 判断,如果集合里为自定义对象,必须复写 equals 方法List<Integer> list = Arrays.asList(1,2,3,3,9,8);
// 过滤大于1的元素的结果上去重
list = list.stream().filter(v -> v > 1).distinct().collect(Collectors.toList());
// 2,3,9,8sorted - 排序
a - b 为正序,b - a为倒序List<Integer> list = Arrays.asList(1,2,3,3,9,8);
// 正序排序
list = list.stream().sorted((x,y) - > x - y).collect(Collectors.toList()); // 2,3,8,9
// 倒序排序
list = list.stream().sorted((x,y) - > y - x).collect(Collectors.toList());Comparator.comparing 方法 - 推荐List<Integer> list = Arrays.asList(1,2,3,3,9,8);
// 正序
list = list.stream().sorted(Comparator.comparing(x -> x)).collect(Collectors.toList());
// 按照 Person 对象的 age 属性正序排序
list = list.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
// reversed 方法为倒序
list = list.stream().sorted(Comparator.comparing(x -> x).reversed()).collect(Collectors.toList());thenComparing 自定义排序条件List<Integer> list = Arrays.asList(1,2,3,3,9,8);
// 正序排序,如果存在相等的值会调用 thenComparing 里的逻辑,按照 fun 函数的返回值排序
list = list.stream().sorted(Comparator.comparing(x -> x)
.thenComparing(x -> fun(x))).collect(Collectors.toList());limit - 截取
截取前n个元素
List<Integer> list = Arrays.asList(1,2,3,3,9,8);
// 截取前1个元素
list = list.stream().limit(1).collect(Collectors.toList()); // 1skip - 跳过
跳过前n个元素
List<Integer> list = Arrays.asList(1,2,3,3,9,8);
// 截取前1个元素
list = list.stream().skip(1).collect(Collectors.toList()); // 2,3,3,9,8map - 映射
将集合中的元素 T 映射为 R
// 初始化数据省略
// list = {"name":"jack","age":19},{"name":"alice","age":18}]
// ageList 为 list 集合里每个 Person 对象的 age 属性的集合
List<Integer> ageList = list.stream().map(Person::getAge).collect(Collectors.toList()); // 19,18
List<String> ageList = list.stream().map(Person::getName).collect(Collectors.toList()); // jack,aliceflatMap - 合并
List<String> list = new ArrayList<>();
list.add("aaa bbb ccc");
list.add("ddd eee fff");
list.add("ggg hhh iii");
// map 方法里的 split 方法把每个元素分割为数组,整个集合变为二维数组,大致结构如下
// [["aaa","bbb","ccc"],["ddd","eee","fff"],["ggg","hhh","iii"]]
// flatMap 方法把每个元素提取出来合并成一个一维数组
list = list.stream().map(s -> s.split(" ")).flatMap(Arrays::stream).collect(toList());
// ["aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","iii"]anyMatch - 存在
流中是否 有一个 元素符合给定的条件
// list = {"name":"jack","age":19},{"name":"alice","age":18}]
boolean b = list.stream().anyMatch(person -> person.getAge() == 20);
// falseallMatch - 检查
流中是否 所有 元素符合给定的条件
boolean b = list.stream().anyMatch(person -> person.getAge() < 20); // truenoneMatch - 逆查
流中是否 没有 元素符合给定的条件
boolean b = list.stream().noneMatch(person -> person.getAge() < 20); // falsereduce - 组合
组合流中的元素,可以求和、求积、求最大值
// list = [18,19]
// 求和,第一个参数为初始值,第二个为 lambda 表达式
int sum = list.stream().reduce(0, (x, y) -> x + y) // 37
// 求积
int multiplication = list.stream().reduce(1, (x, y) -> x * y) // 342forEach - 循环
// list = [18,19]
// 遍历打印元素
list.forEach(System.out::println) // 18,1summaryStatistics - 统计
主要用于int、double、long等基本类型上
getMax- 获取最大值
getMin- 获取最小值
getSum- 获取和
getAverage- 获取平均值
// list = [1,2,3]
int sum = list.stream().mapToInt(x -> x).summaryStatistics().getSum(); // 6concat - 组合
使用
Stream 类的 concat 静态方法组合两个流Stream<Integer> stream = Stream.concat(Stream.of(1,2,3),Stream(7,8,9)); // 1,2,3,7,8,9