在list<map>中,根据map某几个key值相等的累加


将list中的数据按map中的某几个key值合计

    public static void main(String[] args) {
        List> list = new ArrayList>();
        Map map1 = new HashMap();
        map1.put("month", "1");
        map1.put("统计维度", "延期未发货");
        map1.put("发货额", 1289);
        list.add(map1);

        Map map2 = new HashMap();
        map2.put("month", "1");
        map2.put("统计维度", "延期未发货");
        map2.put("发货额", 1289);
        list.add(map2);

        Map map3 = new HashMap();
        map3.put("month", "1");
        map3.put("统计维度", " 当月新增未发货");
        map3.put("发货额", 1289);
        list.add(map3);

        Map map4 = new HashMap();
        map4.put("month", "2");
        map4.put("统计维度", " 当月新增未发货");
        map4.put("发货额", 1289);
        list.add(map4);

        Map map5 = new HashMap();
        map5.put("month", "2");
        map5.put("统计维度", " 当月新增未发货");
        map5.put("发货额", 1289);
        list.add(map5);

        Map map6 = new HashMap();
        map6.put("month", "2");
        map6.put("统计维度", "延期未发货");
        map6.put("发货额", 1289);
        list.add(map6);
        System.out.println("合并前的数据--->>>"+list);
        //根据month和统计维度两个key 合计发货额
        Map> result = new HashMap>();
        List>  allList = new ArrayList<>();
        for(Map map : list){
            String id = map.get("month").toString();
            String date = map.get("统计维度").toString();
            String newId  = id+date;
            Long value = Long.parseLong(map.get("发货额").toString());
            if(result.containsKey(newId)){
                Long temp = Long.parseLong(result.get(newId).get("发货额").toString());
                value += temp;
                result.get(newId).put("发货额", value);
                continue;
            }
            result.put(newId, map);
            allList.add(map);
        }
        System.out.println("合并后的数据--->>>"+allList);
    }

实现效果:

合并前的数据--->>>[{month=1, 发货额=1289, 统计维度=延期未发货}, {month=1, 发货额=1289, 统计维度=延期未发货}, {month=1, 发货额=1289, 统计维度= 当月新增未发货}, 
          {month=2, 发货额=1289, 统计维度= 当月新增未发货}, {month=2, 发货额=1289, 统计维度= 当月新增未发货}, {month=2, 发货额=1289, 统计维度=延期未发货}]
合并后的数据
--->>>[{month=1, 发货额=2578, 统计维度=延期未发货}, {month=1, 发货额=1289, 统计维度= 当月新增未发货},
          {month=2, 发货额=2578, 统计维度= 当月新增未发货}, {month=2, 发货额=1289, 统计维度=延期未发货}]