如何将 List<Map<String, String>> 转换为 List<Map<String, Map<String, String>>>

how to convert List<Map<String, String>> to List<Map<String, Map<String, String>>>

谁能告诉我如何将List<Map<String, String>>转换为List<Map<String, Map<String, String>>>,转换后的列表映射对象中的键是列表映射中的值之一。

简而言之,我正在努力实现以下目标。但我在列表中得到空值。

List<Object> finalStatus = status.stream().map(map->new HashMap<>().put(map.get("testcase_mapping_run_id"), map)).collect(Collectors.toList()); ```
            
            

问题是map(map->new HashMap<>().put(map.get("testcase_mapping_run_id"), map))。该 lambda 不 return 新创建的地图。它 returns 任何 put returns,即 the previous value of the key in the map。由于地图是空的,map 总是 returns null.

所以你想要的大概是

List<Map<String, Map<String, String>>> = status.stream()
    .map(map->{
        Map<String, Map<String, String>> newMap = new HashMap<>();
        newMap.put(map.get("testcase_mapping_run_id"), map);
        return newMap;
    })
    .collect(Collectors.toList());