当前位置: 首页 > ai >正文

JDK8中的 Stream流式编程用法优化(工具类在文章最后)

Java从JDK8起提供了Stream流这个功能,于是项目里出现了大量基于Stream流的写法。随着项目的进行,慢慢的代码中铺天盖地的都是下面的写法:

 List<User> userList = null;if (condition) {userList = new ArrayList<>();userList.add(User.builder().id(1L).name("张三").age(25).build());userList.add(User.builder().id(2L).name("李四").age(30).build());userList.add(User.builder().id(3L).name("王五").age(40).build());}...省略了的业务逻辑...List<User> users = userList.stream().filter(it-> it.getAge() > 28).collect(Collectors.toList());System.out.println(users);

上面的代码中,构建了一个包含3个User对象的List,然后取出了其中age大于28的User对象构成了新的List。相信如果在日常工作和学习中写过了大量的类似的代码,很容易能看出问题来,userList会出现为空的情况,这时候是很容易触发空指针异常的。所以我们在使用stream前通常都是需要做非空判断的。于是,优化后的代码出现了:

List<User> userList = null;
if (condition) {userList = new ArrayList<>();userList.add(User.builder().id(1L).name("张三").age(25).build());userList.add(User.builder().id(2L).name("李四").age(30).build());userList.add(User.builder().id(3L).name("王五").age(40).build());
}...省略了的业务逻辑...
if (userList != null) {List<User> users = userList.stream().filter(it-> it.getAge() > 28).collect(Collectors.toList());System.out.println(users);
}

更近一步,还可以这样写:

List<User> userList = null;
if (condition) {userList = new ArrayList<>();userList.add(User.builder().id(1L).name("张三").age(25).build());userList.add(User.builder().id(2L).name("李四").age(30).build());userList.add(User.builder().id(3L).name("王五").age(40).build());
}...省略了的业务逻辑...
List<User> users = Optional.of(userList).orElse(List.of()).stream().filter(it-> it.getAge() > 28).collect(Collectors.toList());
System.out.println(users);

到了这里,这已经是一段比较常规的业务代码了,但是它真的很丑有没有。作为一个业务研发人员,我为什么要考虑这么多呢?我不需要关注List是怎么转换成stream的,同样的,我也不关心为什么在使用完stream以后还要collect。于是,我将上面的流式代码封装成了工具类,上面的代码可以简化为:

List<User> userList = null;
if (condition) {userList = new ArrayList<>();userList.add(User.builder().id(1L).name("张三").age(25).build());userList.add(User.builder().id(2L).name("李四").age(30).build());userList.add(User.builder().id(3L).name("王五").age(40).build());
}...省略了的业务逻辑...
List<User> users = Streams.filter(userList, it-> it.getAge() > 28);
System.out.println(users);

同样的,也支持其他操作:

//返回年龄大于28的用户列表
List<User> filterUsers = Streams.filter(userList, it -> it.getAge() > 28);//返回年龄大于28的用户的姓名列表
List<String> filterNames = Streams.toList(userList, it -> it.getAge() > 28, User::getName);//返回年龄大于28的用户的姓名作为key, 年龄只作为value的map
Map<String, Integer> filterNameAndAgeMap = Streams.toMap(userList, it -> it.getAge() > 28, User::getName, User::getAge);//返回所有用户的姓名列表
List<String> allUserNames = Streams.toList(userList, User::getName);//返回所有用户的姓名作为key, 用户信息只作为value的map
Map<String, User> allUserMap = Streams.mappingToMap(userList, User::getName);//返回所有用户的姓名作为key, 年龄作为value的map
Map<String, Integer> nameAndAgeMap = Streams.toMap(userList, User::getName, User::getAge);//返回年龄大于28的第一个用户, 如果找不到则使用默认值
User user = Streams.findFirstOrElse(userList, it-> it.getAge() > 28, new User());//返回年龄大于28的第一个用户, 如果找不到则抛出异常
User user2 = Streams.findFirstOrThrow(userList, it-> it.getAge() > 28, "未找到符合条件的数据");

最后附上工具类源码供各位取用:

package com.zlyx.common.util;import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;public class Streams {public static <T> List<T> filter(List<T> dataList, Predicate<T> filter) {return Optional.of(dataList).orElse(List.of()).stream().filter(filter).collect(Collectors.toList());}public static <T> Set<T> filter(Set<T> dataList, Predicate<T> filter) {return Optional.of(dataList).orElse(Set.of()).stream().filter(filter).collect(Collectors.toSet());}public static <T> T findFirstOrThrow(List<T> dataList) throws Exception {return findFirstOrThrow(dataList, "未找到符合条件的数据");}public static <T> T findFirstOrThrow(List<T> dataList, String errorMsg) throws Exception {return findFirstOrThrow(dataList, Objects::nonNull, errorMsg);}public static <T> T findFirstOrThrow(List<T> dataList, Predicate<T> filter, String errorMsg) throws Exception {return Optional.of(dataList).orElse(List.of()).stream().filter(filter).findFirst().orElseThrow(() -> new Exception(errorMsg));}public static <T> T findFirstOrElse(List<T> dataList, T defaultValue) {return findFirstOrElse(dataList, Objects::nonNull, defaultValue);}public static <T> T findFirstOrElse(List<T> dataList, Predicate<T> filter, T defaultValue) {return Optional.of(dataList).orElse(List.of()).stream().filter(filter).findFirst().orElse(defaultValue);}public static <T, R> R findAnyOrNull(List<T> dataList, Function<T, R> mapper) {return Optional.of(dataList).orElse(List.of()).stream().map(mapper).filter(Objects::nonNull).findAny().orElse(null);}public static <T, R> R findAnyOrThrow(List<T> dataList, Function<T, R> mapper, String errorMsg) throws Exception {return Optional.of(dataList).orElse(List.of()).stream().map(mapper).filter(Objects::nonNull).findAny().orElseThrow(() -> new Exception(errorMsg));}public static <T, R> Set<R> toSet(List<T> dataList, Function<T, R> mapper) {return toSet(dataList, Objects::nonNull, mapper);}public static <T, R> Set<R> toSet(List<T> dataList, Predicate<T> filter, Function<T, R> mapper) {return Optional.of(dataList).orElse(List.of()).stream().filter(filter).map(mapper).collect(Collectors.toSet());}public static <T, R> List<R> toList(List<T> dataList, Function<T, R> mapper) {return toList(dataList, Objects::nonNull, mapper);}public static <T, R> List<R> toList(List<T> dataList, Predicate<T> filter, Function<T, R> mapper) {return Optional.of(dataList).orElse(List.of()).stream().filter(filter).map(mapper).distinct().collect(Collectors.toList());}public static <T, K> Map<K, T> mappingToMap(List<T> dataList, Function<T, K> keyMapper) {return mappingToMap(dataList, Objects::nonNull, keyMapper);}public static <T, K> Map<K, T> mappingToMap(List<T> dataList, Predicate<T> filter, Function<T, K> keyMapper) {return Optional.of(dataList).orElse(List.of()).stream().filter(filter).collect(Collectors.toMap(keyMapper, it -> it, (v1, v2) -> v2));}public static <T, K, V> Map<K, V> toMap(List<T> dataList, Function<T, K> keyMapper, Function<T, V> valueMapper) {return toMap(dataList, Objects::nonNull, keyMapper, valueMapper);}public static <T, K, V> Map<K, V> toMap(List<T> dataList, Predicate<T> filter, Function<T, K> keyMapper, Function<T, V> valueMapper) {return Optional.of(dataList).orElse(List.of()).stream().filter(filter).collect(Collectors.toMap(keyMapper, valueMapper));}
}
http://www.xdnf.cn/news/8164.html

相关文章:

  • ollama接口配合chrome插件实现商品标题和描述生成
  • CLIP阅读笔记
  • 亚远景-ASPICE评估中的常见挑战及应对策略
  • 云蝠智能大模型:深度赋能的智能化功能,是怎么做到的?
  • 深入对比分析 Python 中字典和集合 异同
  • 高等数学-曲线积分与曲面积分
  • SpringBoot 对象转换 MapStruct
  • 《函数指针数组:创建与使用指南》
  • 【T2I】Controllable Generation with Text-to-ImageDiffusion Models: A Survey
  • 嵌入式学习笔记 D25 :标准i/o操作(2)、文件i/o
  • 2025年5月通信科技领域周报(5.12-5.18):6G太赫兹技术商用突破 空天地一体化网络进入规模部署期
  • Windows解除占用(解除文件占用、解除目录占用)查看文件进程(查看父进程、查看子进程、查看父子进程)占用文件占用、占用目录占用
  • 纳斯达克与标普500的技术博弈:解析美股交易系统的低延迟与高安全解决方案
  • 基于SpringBoot的动漫交流与推荐平台-036
  • 【学习笔记】计算机操作系统(五)—— 虚拟存储器
  • 数据库5——审计及触发器
  • 模拟地和数字地的连接方式
  • Java中的大根堆与小根堆
  • 无人机避障——深蓝学院浙大Ego-Planner规划部分
  • 工具看点 | 澳鹏多模态标注工具:构建AI认知的语义桥梁
  • 第四十五节:目标检测与跟踪-Meanshift/Camshift 算法
  • MCP Server Resource 开发学习文档
  • 记一次奇葩的错误,uniapp @tap点击失效
  • Nockchain项目部署教程
  • 从连接中枢到终端接入——解析工业无线AP与客户端的协同之道
  • 安装部署配置jenkins
  • Nginx 1.25.4交叉编译问题:编译器路径与aclocal.m4错误解决方案
  • wifi 如果检查失败,UI 就会出现延迟或缺失打勾的现象。
  • linux中部署jdk,开机自启动jdk以及linux中java开机自启某个jar包文件
  • 算法第26天 | 贪心算法、455.分发饼干、376. 摆动序列、 53. 最大子序和