java 将list<Map>转成map
时间: 2023-11-26 13:09:20 浏览: 76
java 三种将list转换为map的方法详解
可以使用Java 8的Stream API来实现,示例代码如下:
```
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
map1.put("key1", "value1");
map1.put("key2", "value2");
list.add(map1);
Map<String, Object> map2 = new HashMap<>();
map2.put("key3", "value3");
map2.put("key4", "value4");
list.add(map2);
Map<String, Object> result = list.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println(result);
```
输出结果为:
```
{key1=value1, key2=value2, key3=value3, key4=value4}
```
阅读全文