List根据相同key值去重并计算其他key对应的value之和

public static void main(String[] args) {
List<Map<String,Object>> mapList = new ArrayList<>();
Map map1 = new HashMap();
map1.put(“name”,“张三”);
map1.put(“age”,1);
map1.put(“school”,“北京大学”);

    Map map2 = new HashMap();
    map2.put("name","张三");
    map2.put("age",2);
    map2.put("school","清华大学");

    Map map3 = new HashMap();
    map3.put("name","张三");
    map3.put("age",3);
    map3.put("school","青岛大学");

    Map map4 = new HashMap();
    map4.put("name","李四");
    map4.put("age",9);
    map4.put("school","人民大学");

    mapList.add(map1);
    mapList.add(map2);
    mapList.add(map3);
    mapList.add(map4);
    List<Map<String,Object>> newMapList = new ArrayList<>();
    for (int i = 0; i <mapList.size() ; i++) {
        String oldName = (String) mapList.get(i).get("name");
        boolean flag = false;
        for (int j = 0; j < newMapList.size(); j++) {
            String newName = (String) newMapList.get(j).get("name");
            if(newName.equals(oldName)){
               System.out.println("mapList  age ===" + mapList.get(i).get("age") );
                int totalAge = (int) mapList.get(i).get("age") + (int)newMapList.get(j).get("age");
                newMapList.get(j).put("age",totalAge);
                flag = true;
            }
        }
        if(flag == false){
            newMapList.add(mapList.get(i));
		}
    }
    System.out.println(newMapList);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值