news 2026/5/13 5:04:08

Springboot利用Stream过滤集合方法总结

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Springboot利用Stream过滤集合方法总结

1、获取集合元素中指定属性值的集合

List<String>columnNames=list.stream().map(DataDto::getName).collect(Collectors.toList());

2、过滤集合中符合指定过滤条件的元素集合

例如获取list集合中,id等于“1000”,title等于“安排情况”的所有元素集合。

List<DataDto>filterList=list.stream().filter(s->s.getId().equals("1000")).filter(s->s.getTitle1().equals("安排情况")).collect(Collectors.toList());

3、利用stream给集合去重

List<String>filterList=list.stream().distinct().collect(Collectors.toList());

4、获取集合中元素某个属性的集合并去重

List<Double>filterList=list.stream().map(DataDto::getMount).distinct().collect(Collectors.toList());

5、获取集合元素指定属性的合计值

DoublesumResult=list.stream().mapToDouble(n->Double.parseDouble(n.getMount()==null?"0":n.getMount())).summaryStatistics().getSum();

6、按照集合元素某一个属性值排序

从小到大排序

List<Person>sortedList=people.stream().sorted(Comparator.comparing(Person::getAge))// 按age属性升序.collect(Collectors.toList());

从大到小排序

List<Person>sorted=people.stream().sorted(Comparator.comparing(Person::getAge).reversed()).collect(Collectors.toList());

7、根据集合元素的某一个属性值去重,保持原有顺序

例如:根据dataList元素中的id属性去重,dataList数据如下:
1、DataDto(id=3, name=“张三”)
2、DataDto(id=2, name=“李四”)
3、DataDto(id=3, name=“王五”) ← 和第1条id重复,删除
4、DataDto(id=1, name=“赵六”)
去重后的结果:
1、 DataDto(id=3, name=“张三”)
2、 DataDto(id=2, name=“李四”)
3、DataDto(id=1, name=“赵六”)

List<DataDto>dataList=dataList.stream().collect(Collectors.toCollection(()->newTreeSet<>(Comparator.comparing(DataDto::getId)))).stream().collect(Collectors.toList());
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/13 4:59:41

VLA-Adapter LoRA微调技术详解:如何在有限资源下实现最佳性能

VLA-Adapter LoRA微调技术详解&#xff1a;如何在有限资源下实现最佳性能 【免费下载链接】VLA-Adapter VLA-Adapter: An Effective Paradigm for Tiny-Scale Vision-Language-Action Model 项目地址: https://gitcode.com/gh_mirrors/vl/VLA-Adapter VLA-Adapter是一个…

作者头像 李华
网站建设 2026/5/13 4:58:42

终极RedwoodJS性能基准测试:10个关键指标揭示GraphQL框架性能优势

终极RedwoodJS性能基准测试&#xff1a;10个关键指标揭示GraphQL框架性能优势 【免费下载链接】redwood RedwoodGraphQL 项目地址: https://gitcode.com/gh_mirrors/re/redwood RedwoodJS作为现代GraphQL全栈框架&#xff0c;凭借其独特的架构设计和优化策略&#xff0c…

作者头像 李华
网站建设 2026/5/13 4:54:07

EUV光刻技术中的光晕与阴影效应解析

1. EUV光刻中的光晕与阴影效应解析在半导体制造领域&#xff0c;极紫外光刻(EUV)技术已成为突破22nm以下制程节点的关键利器。作为一名从业十余年的光刻工程师&#xff0c;我深刻体会到这项技术带来的革命性变化——13.5nm的极短波长让我们重新获得了k1>0.5的光学成像条件。…

作者头像 李华
网站建设 2026/5/13 4:45:08

SNKRX进阶攻略:如何打造无敌英雄蛇阵容的终极指南

SNKRX进阶攻略&#xff1a;如何打造无敌英雄蛇阵容的终极指南 【免费下载链接】SNKRX A replayable arcade shooter where you control a snake of heroes. 项目地址: https://gitcode.com/gh_mirrors/sn/SNKRX SNKRX是一款创新的街机射击roguelite游戏&#xff0c;你将…

作者头像 李华