java8分组统计 map groupBy

按多个字段分组统计

map

 List collect = ms.stream().collect(Collectors.toMap(k -> k.getG1()+"_"+k.getG2()+"_"+k.getG3(), e -> e, (o1, o2) -> {
                         	 o1.setK1(o1.getK1().add(o2.getK1()).setScale(2,BigDecimal.ROUND_HALF_UP));
            o1.setK2l(o1.getK2.add(o2.getK2).setScale(2,BigDecimal.ROUND_HALF_UP));
            return o1;
        })).values().stream().collect(Collectors.toList());

groupBy

public class StringUtil {

public static String format(String value, Object... paras) {
    return MessageFormat.format(value, paras);
}

}

 Map> m1= ms.stream().collect(
                Collectors.groupingBy(a -> StringUtil.format("{0}#{1}", a.getG1(),a.getG2())));

        List collect1 = new ArrayList<>();
        materierlDlMap.forEach((k,v)->{
            //如果公司有多个值 需要增加合计行
            if(v.size()>=1){
                BigDecimal s= v.stream().map(o->o.getK0qmkcje()!=null?o.getK0qmkcje():BigDecimal.ZERO).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal s2 = v.stream().map(o->o.getK0qmkcsl()!=null?o.getK0qmkcsl():BigDecimal.ZERO).reduce(BigDecimal.ZERO, BigDecimal::add);
                M hj = new M();

              
                hj.setK1l(stockSlReduce.setScale(0,BigDecimal.ROUND_HALF_UP));
                hj.setK2(stockReduce.setScale(0,BigDecimal.ROUND_HALF_UP));
                hj.setRadio("0");
                collect1.add(hj);
            }
        });

你可能感兴趣的:(java,spring,boot,spring,java)