List集合流处理类型小结
2023-04-18 14:06:08 时间
本文为博主原创,未经允许不得转载
对应实体类
import lombok.Getter; import lombok.Setter; @Getter @Setter public class Student { private String name; private int age; private String className; private String birthday; }
1.根据字段取出某一个字段属性的集合
List<Student> studentList = new ArrayList<>(); List<int> newList = studentList.stream().map(Student::getAge).collect(Collectors.toList()); for (Student student : newList) { System.out.println(student.getName()+"---"+student.getAge()); }
2。List根据某个字段升序排序
List<Student> studentList = new ArrayList<>(); List<Student> newList = studentList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList()); for (Student student : newList) { System.out.println(student.getName()+"---"+student.getAge()); }
3.List根据某个字段排序降序
List<Student> list = new ArrayList<>(); list = list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
4.获取某一字段属性值对应的数据集合
List<Student> resultList = studentList.stream() .filter((Student stu) -> area.equals(stu.getAge())) .collect(Collectors.toList());
5.根据多个字段排序
List<Student> list = list.sorted(Comparator.comparing(Student::getName).thenComparing(Student::getAge)).collect(Collectors.toList());
6.根据某个字段值获取出对应的对象
Student stu = studentList.stream() .filter(p -> "2018-08-12 12:10".equals(p.getBirthday())) .findAny().orElse(null);
7.对集合元素去重
List<String> nameList = new ArrayList<>(); nameList = nameList.stream().distinct().collect(Collectors.toList());
8.对集合某一个属性进行求和
List<Student> stuList = new ArrayList<>(); double totalAge = stuList.stream().collect(Collectors.summingDouble(Student::getAge));
9.list 封装为map ,key为 className;value 为student对象
Map<Long, Student> studentMap = studentList.stream().collect(Collectors.toMap(Student::getClassName, Function.identity()));
当我们使用 Stream 时,要将它转换成其他容器或Map。这时候,就会使用到 Function.identity( ) 。
Function.identity() 返回一个输出跟输入一样的Lambda表达式对象,等价于形如 t -> t
形式的Lambda表达式。是一个用来返回自己的lambda表达式
10.多级排序
studentList = studentList.stream().sorted(Comparator.comparing(Student::getClassName).thenComparing(PropConfig::getAge)).collect(Collectors.toList());
11.获取集合中的某一个属性的数据集合并去重
// 所有的ip信息对象集合
List<NetiIpInfo> netInfoList = netIpService.queryNetIpList();
// 从所有IP信息对象集合中根据机房id过滤出所有机房id不同的数据对象,并根据机房id去重 List<NetiIpInfo> distinctIpRoomList = netInfoList.stream().collect(Collectors .collectingAndThen(Collectors.toCollection(() -> new TreeSet<>( Comparator.comparing(NetiIpInfo::getIpRoomId))), ArrayList::new));
代码是很简答,很优雅的
解释一下
list.stream(): 是把list集合转化为stream集合
sorted(): 进行排序,其中Comparator.comparing(Student::getAge)表示按照年纪排序,
.reversed()表示是逆序,因为默认情况下,不加.reversed 是升序的
collect(Collectors.toList()): 再次将排序后的stream集合转化为list集合
.findAny()表示将其中任意一个返回;
.orElse(null)表示如果一个都没找到返回null
distinct() 对集合元素或对象去重
summingDouble() 对集合元素进行求和为double类型数据
相关文章
- 操作系统核心原理-5.内存管理(上):基本内存管理
- 如何检索我的进程的已保存的用户ID?
- Scrum Guide - Scrum指南中文版
- 在UNIX目录下与每个文件的循环调用弹簧批处理程序作为参数
- 操作系统核心原理-4.线程原理(下):死锁基础原理
- 操作系统核心原理-4.线程原理(上):线程基础与线程同步
- Unix变量声明
- 操作系统核心原理-3.进程原理(下):进程通信
- DynamicProxy生成速度
- 操作系统核心原理-3.进程原理(中):进程调度
- 操作系统核心原理-2.一些基本概念
- Windows Phone应用程序中的组合根在哪里?
- 操作系统核心原理-1.操作系统导论
- 如何注入依赖于层次结构Spring.Net
- 如何解析来自LightCore ServiceLocator的命名实例?
- 《代码的未来》读书笔记:也谈闭包
- 《代码的未来》读书笔记:内存管理与GC那点事儿
- Silverlight 4 + WCF RIA Services + Prism v4为什么我需要模块依赖于我的shell?
- Coldfusion 9 CFIMAGE ICC配置文件 - 不支持,为什么?如何解决?
- 轻量级ORM框架初探-Dapper与PetaPoco的基本使用