1.一个一个项目的分组合计
list.stream().collect(
Collectors..groupingBy(
Bean::getItemA
,Collectors..groupingBy(
Bean::getItemB,
Collectors..reducing(
new Bean(),
(oldBean,newBean) -> {
Bean oldBeanA = (Bean)oldBean;
Bean newBeanA = (Bean)newBean;
if (StringUtils.isEmpty(oldBeanA .getItemA())) {
return newBeanA;
}
oldBeanA.setItemC(oldBeanA.getItemC()+newBeanA.getItemC());
oldBeanA.setItemD(oldBeanA.getItemD()+newBeanA.getItemD());
return oldBeanA;
}
)
)
)
)
2.利用复合Key实现多项目分组合计
list.stream().collect(
Collectors.groupingBy(
bean -> bean.getItemA() + bean.getItemB(),
Collectors.reducing(内容与上面的处理相同)
)
)