栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Stream流操作集合的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Stream流操作集合的方法

Stream流底层会通过迭代器Iterator的方式过滤你需要过滤的属性集合

1.Stream是元素的集合,这点让Stream看起来用些类似Iterator;
2.可以支持顺序和并行的对原Stream进行汇聚的操作;

1.根据对象属性过滤list中重复对象
ArrayList agentCollect = agentList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Agent::getAgentName))), ArrayList::new));

2.根据对象属性过滤list中重复对象多个属性
ArrayList agentToCsoCollect = agentToCsoList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>( Comparator.comparing(p->p.getAgentPromotionId() + “;” + p.getCsoPromotionId()))), ArrayList::new));

3.获取list中属性相同得对象,contains取两个集合交集
List list = hashSet.stream().filter(s -> integers.contains(s.getSn())).collect(Collectors.toList());

4.获取list中属性不相同得对象
List list = hashSet.stream().filter(s ->!integers.contains(s.getSn())).collect(Collectors.toList());

5.根据list中对象属性排序
List checkExportList = list.stream().sorted(Comparator.comparing(CheckExport::getTime)).collect(Collectors.toList());

6.获取list集合中对象某一个属性形成集合
List list2 = list.stream().map(A::getAge).collect(Collectors.toList());//未去重
List list3 = list.stream().map(A::getAge).distinct().collect(Collectors.toList());//已去重

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/643912.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号