1、获取年龄>20的人员列表
List<User> list = users.stream().filter(item -> item.getAge() != null && item.getAge() > 20).collect(Collectors.toList());
2、以ID(id+userName)为Key,用户对象为Value构建Map(key为NULL时报错)
Map<Long, User> map = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));
Map<String, User> map = users.stream().collect(Collectors.toMap(user -> String.valueof(user.getId() + user.getUserName()), Function.identity()));
3、以ID为Key,deptId为Value构建Map(id或deptId为NULL时报错)
Map<Long, Long> map = users.stream().collect(Collectors.toMap(User::getId, User::getDeptId));
4、以deptId为Key进行分组(deptId为NULL时报错)
Map<Long, List<User>> map = users.stream().collect(Collectors.groupingBy(User::getDeptId));
5、提取ID 集合(List)
List<Long> ids = users.stream().map(User::getId).collect(Collectors.toList());
6、提取部门ID集合(List)并去重
List<Long> deptIds = users.stream().map(User::getDeptId).distinct().collect(Collectors.toList());
7、提取部门ID集合(Set)
Set<Long> deptIds = users.stream().map(User::getDeptId).collect(Collectors.toSet());
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2018-07-13 Redis在CentOS7中的启动警告