java 8中如何加入或合并HashMap的List

How to join or merge List of HashMap in java 8

我有散列图列表,如果下面的键相同,我想加入值我已经提到了输入和预期输出。我已经解决了很多与此问题相关的问题答案仍然找不到解决方案。

输入:

[
  {
    "id": "316",
    "text1": true
  },
  {
    "id": "316",
    "text2": true
  },
  {
    "id": "315",
    "text1": true
  },
  {
    "id": "315",
    "text2": true
  }
]

输出:

[
  {
    "id": "316",
    "text1": true,
    "text2": true
  },
  {
    "id": "315",
    "text1": true,
    "text2": true
  }
]

代码片段:

                    List list1= new ArrayList();
                    HashMap details= new HashMap();        
                    details.put("id", "305");
                    details.put("text1",true);    
                    list1.add(details); 
                    HashMap details2= new HashMap();
                    details2.put("id", "305");
                    details2.put("text2",true);              
        
                    list1.add(details2);       
        

根据@Thomas 的评论,您可以这样做:

Map<String, Map<String, Object>> mapMap = new HashMap<>();
    list.forEach(map -> map.entrySet().forEach(entry ->
            mapMap.merge((String) map.get("id"), map, (map1, map2) -> {
                map1.putAll(map2);
                return map1;
            }))
    );

System.out.println(mapMap.values());