作者: whooyun发表于: 2023-09-08 18:14
////////////数组中某个字段匹配,N(1)
Map<Integer, String> orgMap = orgList.stream().collect(Collectors.toMap(ESBasOrg::getId, ESBasOrg::getName));
///////////List中某个属性值从0累加
BigDecimal freeAmt = orgValue.stream().map(FudAccountPrepayVO::getFreeAmt).reduce(BigDecimal.ZERO, BigDecimal::add);
//////////排序
List<AdsPrereceiptAmtDetailVO> adsPrereceiptAmtDetailVOS = list.stream()
.sorted(Comparator.comparing(AdsPrereceiptAmtDetailVO::getStatDtm))
.collect(Collectors.toList());
////////// 使用Java 8流来对id相同的对象进行分组和合计money字段
Map<Integer, BigDecimal> idToTotalMoneyMap = simpleVOList.stream()
.collect(Collectors.groupingBy(
AcvGdsSimpleVO::getGoodsOrgId,
Collectors.mapping(AcvGdsSimpleVO::getStoreCnt, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))
));
//////////对象去重,并保留一个
ArrayList<AcvGdsSimpleVO> distinctList = simpleVOList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(AcvGdsSimpleVO::getGoodsOrgId))), ArrayList::new));
distinctList.forEach(simpleVO -> {
simpleVO.setStoreCnt(idToTotalMoneyMap.get(simpleVO.getGoodsOrgId()));
});
////////// 多字段分组,map的key为数组的情况获取到分组后的数组
Map<List<? extends Number>, List<AdsStkGdsOrgBalCnt1dVO>> listMap = list.stream().collect(Collectors.groupingBy(vo -> Arrays.asList(vo.getSkuId(), vo.getOwnOrgId())));
List<? extends Number> key = new ArrayList<>();
for (StoStockVO stockVO : stockVOList) {
key = Arrays.asList(stockVO.getSkuId(), stockVO.getOwnOrgId());
if (listMap.containsKey(key)) {
List<AdsStkGdsOrgBalCnt1dVO> adsStkGdsOrgBalCnt1dVOS = listMap.get(key);
if (!CollectionUtils.isEmpty(adsStkGdsOrgBalCnt1dVOS)) {
adsStkGdsOrgBalCnt1dVOS.sort(Comparator.comparing(AdsStkGdsOrgBalCnt1dVO::getStatDtm));
Optional<AdsStkGdsOrgBalCnt1dVO> first = adsStkGdsOrgBalCnt1dVOS.stream().findFirst();
if (stockVO.getSkuId().equals(first.get().getSkuId()) && stockVO.getOwnOrgId().equals(first.get().getOwnOrgId())) {
List<LocalDate> dates = new ArrayList<>();
adsStkGdsOrgBalCnt1dVOS.forEach(item->{
dates.add(LocalDate.parse(item.getStatDtm(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
});
int count = findLongestConsecutiveDays(dates);
stockVO.setNegativeDays(count);
}
}
}
}