分組:
Map<Long,List<PmsCategoryStatisticVo>> map = list.stream().collect(Collectors.groupingBy(b -> b.getCategoryId()));
list爲對象集合,根據categoryId分組,key爲cateogryId,value爲categoryId相同的對象集合。
對象屬性相加:
BigDecimal amount = list.stream()
// 將對象的mongey取出來map爲Bigdecimal
.map(b -> b.getAmount())
// 使用reduce聚合函數,實現累加器
.reduce(BigDecimal.ZERO, BigDecimal::add);
將對象的mongey取出來map爲Bigdecimal,使用reduce聚合函數,實現累加器
篩選並根據id去重:
List list= statisticList.stream()
.filter(b -> '2019-01-01'.equals(b.getStatisticTime()))
.filter(b -> b.getCategoryId().equals(1L))
.collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(b -> b.getId()))), ArrayList::new));
過濾statisticTime等於2019-01-01,
過濾categoryId等於1L,
去重->將過濾後的轉set,key爲id(set集合id不能重複)->在轉爲ArrayList