通过流将地图列表转换为单个地图

新手上路,请多包涵

我在数据库中查询两列,其中第一列是第二列的键。如何将结果列表转换为单个地图?有可能吗?我刚刚看到了豆子的例子。

 List<Map<String, Object>> steps = jdbcTemplate.queryForList(
        "SELECT key, value FROM table");

// well this doesn't work
Map<String, String> result = steps.stream()
        .collect(Collectors.toMap(s -> s.get("key"), s -> s.get("value")));

原文由 ave4496 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 266
2 个回答

您忘记将键和值映射转换为生成 String

 final Map<String, String> result = steps
                .stream()
                .collect(Collectors.toMap(s -> (String) s.get("key"),
                                          s -> (String) s.get("value")));

完整示例

public static void main(String[] args) {
    final List<Map<String, Object>> steps = queryForList("SELECT key, value FROM table");
    final Map<String, String> result = steps
            .stream()
            .collect(Collectors.toMap(s -> (String) s.get("key"), s -> (String) s.get("value")));
    result.entrySet().forEach(e -> System.out.println(e.getKey() + " -> " + e.getValue()));
}

private static List<Map<String, Object>> queryForList(String s) {
    final List<Map<String, Object>> result = new ArrayList<>();

    for (int i = 0; i < 10; i++) {
        final Map<String, Object> map = new HashMap<>();
        map.put("key", "key" + i);
        map.put("value", "value" + i);
        result.add(map);
    }

    return result;
}

哪个打印

key1 -> value1
key2 -> value2
key0 -> value0
key5 -> value5
key6 -> value6
key3 -> value3
key4 -> value4
key9 -> value9
key7 -> value7
key8 -> value8

原文由 esin88 发布,翻译遵循 CC BY-SA 4.0 许可协议

我们可以使用 java 流的 reduce() 将映射列表转换为 java 中的单个映射。

请检查以下代码以了解其使用方式。

例如:

 @Data
@AllArgsConstructor
public class Employee {

    private String employeeId;
    private String employeeName;
    private Map<String,Object> employeeMap;
}

public class Test{
 public static void main(String[] args) {

        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();


        map1.put("salary", 1000);
        Employee e1 = new Employee("e1", "employee1", map1);

        map2.put("department", "HR");
        Employee e2 = new Employee("e2", "employee2", map2);

        map3.put("leave balance", 14);
        Employee e3 = new Employee("e3", "employee3", map3);

        //now we create a employees list and add the employees e1,e2 and e3.
        List<Employee> employeeList = Arrays.asList(e1,e2,e3);

        //now we retreive employeeMap from all employee objects and therefore have a List of employee maps.
        List<Map<String, Object>> employeeMaps = employeeList
        .stream()
        .map(Employee::getEmployeeMap)
        .collect(Collectors.toList());

        System.out.println("List of employee maps: " + employeeMaps);

        // to reduce a list of maps to a single map, we use the reduce function of stream.

        Map<String, Object> finalMap = employeeMaps
        .stream()
        .reduce((firstMap, secondMap) -> {
                firstMap.putAll(secondMap);
                 return firstMap;
              }).orElse(null);

        System.out.println("final Map: "+ finalMap);

}
}

输出:员工地图列表:[{salary=1000}, {department=HR}, {leave balance=14}]。

最终地图:{salary=1000, department=HR, leave balance=14}

PS:对于扩展答案表示歉意,这是我第一次使用 stackoverflow。谢谢 :-)

原文由 venkatesh RM 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题