Stream流 | 从对象集合中取某个属性的集合 [ 技术杂谈 ]
九叶的小窝 文章 正文
九叶
{{nature("2022-02-28 17:52:32")}}更新1. 原因与问题
1.1 原因
日常开发中有时候需要将某个对象集合的某个属性提取出来作为一个新的集合使用, 所以记录下来
1.2 描述
现在有一个叫 companyEmployeeList
集合 我们需要将里面的 employeeId
提取出来作为一个新的集合
List<CompanyEmployee> companyEmployeeList = companyEmployeeMapper.selectCompanyEmployeeNotUsedById(idArray);
2. 解决方法
2.1 .collect(Collectors.toList()) 转为List集合
List<String> list = companyEmployeeList.stream().map(t -> t.getEmployeeId().toString()).collect(Collectors.toList());
2.2 .toArray 转为数组集合
String[] list = companyEmployeeList.stream().map(t -> t.getEmployeeId().toString()).toArray(String[]::new);
2.3 一个题外小方法
/* 去重 */
companyEmployeeList.stream().distinct().collect(Collectors.toList());
{{nature('2021-12-07 15:29:00')}} {{format('2671')}}人已阅读
{{nature('2021-01-31 20:01:00')}} {{format('1172')}}人已阅读
{{nature('2020-12-11 21:17:00')}} {{format('1085')}}人已阅读
{{nature('2021-04-22 15:16:00')}} {{format('1057')}}人已阅读
目录
标签云
一言
评论 0
{{userInfo.data?.nickname}}
{{userInfo.data?.email}}