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);
}