stream流的常见使用
stream流的常见使用
过滤为空的数据
freeScopeList().stream().
filter(f -> CollectionUtils.isNotEmpty(f.getFreeScopeFeeList())). //免租范围不能空
filter(f -> CollectionUtils.isNotEmpty(f.getFreePeriodList())). //免租期不能空
collect(Collectors.toList()))
收集集合里面的某个字段并去重
// 集团公司id
List list1 = homeBiCompanyDataList.stream().map(map1 -> map1.getGroupCompanyId()).distinct()
.collect(Collectors.toList());
收集集合里面的取某些字段,并用,拼接
String spaceFloorName = spaceDtoList.stream().map(m -> m.getBasFloorDto().getFloorName() + “-” + m.getSpaceName()).collect(Collectors.joining(“,”));
差集
// 最新数据的id
List list1 = boList.stream().map(map1 -> map1.getId()).distinct().collect(Collectors.toList());
// 历史数据的id
List list2 = assetList.stream().map(map1 -> map1.getId()).distinct().collect(Collectors.toList());
// 差集 (list2 - list1) 删除
List deleteList = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
分组
// 先对名字进行分组,然后按照年龄进行分组
Map<String, Map<Object, List>> map = Stream.of(
new Person(“张三”, new Date(), 175),
new Person(“李四”, new Date(), 177),
new Person(“张三”, new Date(), 165),
new Person(“李四”, new Date(), 166),
new Person(“张三”, new Date(), 182))
.collect(Collectors.groupingBy(Person::getName, Collectors.groupingBy(Person::getAge)));
map.forEach((k, v) -> System.out.println(“k=” + k + “\t” + “v=” + v));
list转map
Map<Long, LeaContRuleFeeBo> map =
leaRuleFeeList.stream().collect(Collectors.toMap(LeaContRuleFeeBo::getFeeId, (k) -> k))
forEach 处理list数据
decorationFeeList.stream().forEach(fee -> {
fee.setOpenApproveId(openApproveId);
fee.setIsDeleted(IsDelEnum.IS_UN_DEL.getKey());
});
推荐阅读
-
Conda 的基本使用 - 13.常见问题
-
常见 C# 中的 LINQ 应用:Lambda 表达式使用方法
-
实战经验分享:Vue中的$set使用及其常见问题解析
-
Git submodule 使用中的常见问题与陷阱
-
使用msfvenom创建Windows木马和防杀机制 - 制作常见木马的方法
-
实战体验moment.js时间格式化库的使用与常见问题避免
-
使用Java流对MP4视频文件进行加密,并在H5中播放加密的视频流
-
Vivado中使用Hardware Manager进行JTAG调试的常见问题及解决方法
-
常见问题及解决方案:在使用keep-alive时的include、exclude和max的注意事项
-
Vue项目中正确使用keepAlive和嵌套路由keepAlive的方法,以及常见问题解析