Stream 日常工作笔记
1.LIst 转Map
stream().collect(Collectors.toMap(SplaLineSnDTO::getId, Function.identity()))
2. List对象 先分组 在取其中一个字段 转List
stream().collect(Collectors.groupingBy(SplSnTransHistoryDTO::getInoutLineId, Collectors.mapping(SplSnTransHistoryDTO::getSplSnId, Collectors.toList())))
3. List对象先 分组 在求和
stream().collect(Collectors.groupingBy(OioLineDTO::getSourceLineId, Collectors.mapping(OioLineDTO::getQty, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
4.List对象 平铺 对象的属性
stream().flatMap(e -> e.getSnIds().stream()).collect(Collectors.toList());
List<DoLineDTO> doLineList = doDetailResponse.getList().stream().map(DoDetailDTO::getLines).flatMap(Collection::stream).collect(Collectors.toList());
5.List 对象去重
stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SoLineRespDTO::getGroupId))), ArrayList::new));
6. 将id进行合并nums, sums 相加道回合并后的集合使用Java8的流进行处理
List<BillsNums> result = list.stream()
// 表示id为key, 接着如果有重复的,那么从BillsNums对象o1与o2中筛选出一个,这里选择o1,
// 并把id重复,需要将nums和sums与o1进行合并的o2, 赋值给o1,最后返回o1
.collect(Collectors.toMap(BillsNums::getId, a -> a, (o1,o2)-> {
o1.setNums(o1.getNums() + o2.getNums());
o1.setSums(o1.getSums() + o2.getSums());
return o1;
})).values().stream().collect(Collectors.toList());
return result ;
7判断对象状态是不是全为已完成
oioLineList.stream().allMatch(e -> CommDocStatusEnum.COMPLETED.getCode().equals(e.getLineStatus()))
8.多条件排序
poList = poList.stream().filter(e -> ObjectUtils.isNotNull(e.getCrtTime()) && ObjectUtils.isNotNull(e.getIsDefault())).sorted(Comparator.comparing(ProdInvoiceInfoPO::getIsDefault, Comparator.reverseOrder()).thenComparing(ProdInvoiceInfoPO::getCrtTime, Comparator.reverseOrder())).collect(Collectors.toList());

浙公网安备 33010602011771号