英文:
Java 8 - List<Map> into Single Map
问题
我有一个 List<Map<String, String>>
。我想将它转换为单个映射。
列表的大小可以是1到n。不确定如何在使用Java 8 Stream的情况下执行此操作?
List<Map<String, String>> mapList = new ArrayList<>();
Map<String, String> map1 = new HashMap<>();
map1.put("1", "One");
map1.put("2", "Two");
Map<String, String> map2 = new HashMap<>();
map2.put("3", "Three");
map2.put("4", "Four");
mapList.add(map1);
mapList.add(map2);
Map<String, String> finalMap = new HashMap<>();
在Java 7中,我们可以像这样做:
for (Map<String, String> map : mapList) {
for (String key : map.keySet()) {
finalMap.put(key, map.get(key));
}
}
System.out.println("Final Map " + finalMap); // Final Map {1=One, 2=Two, 3=Three, 4=Four}
英文:
I have a List<Map<String, String>>
. I would like to convert it into single map.
The list size can be 1..n. Not sure how to do that using Java 8 Stream?
List<Map<String, String>> mapList = new ArrayList<>();
Map<String, String> map1 = new HashMap<>();
map1.put("1", "One");
map1.put("2", "Two");
Map<String, String> map2 = new HashMap<>();
map2.put("3", "Three");
map2.put("4", "Four");
mapList.add(map1);
mapList.add(map2);
Map<String, String> finalMap = new HashMap<>();
We could do something like this in Java 7:
for(Map<String, String> map : mapList) {
for(String key : map.keySet()) {
finalMap.put(key, map.get(key));
}
}
System.out.println("Final Map " + finalMap); // Final Map {1=One, 2=Two, 3=Three, 4=Four}
答案1
得分: 8
你可以使用 flatMap
然后使用 Collectors.toMap
:
mapList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
请注意,重复的键没有被处理。如果要忽略重复键,你可以使用:
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1)
英文:
You can flatMap
it and then use Collectors.toMap
:
mapList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Note, that duplicate keys are not handled. To ignore duplicates, you can use:
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1)
答案2
得分: 0
这是一种方法来实现。我选择了一种处理重复项的方法,将它们放入字符串列表中。我在其中一个地图中添加了每个地图的重复项,以进行演示。
List<Map<String, String>> mapList = new ArrayList<>();
Map<String, String> map1 = new HashMap<>();
map1.put("1", "One");
map1.put("2", "Two");
map1.put("4", "Four");
Map<String, String> map2 = new HashMap<>();
map2.put("3", "Three");
map2.put("1", "One");
map2.put("4", "Four");
mapList.add(map1);
mapList.add(map2);
Map<String, List<String>> combined = mapList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(Entry::getKey,
Collectors.mapping(Entry::getValue,
Collectors.toList())));
combined.entrySet().forEach(System.out::println);
输出结果为
1=[One, One]
2=[Two]
3=[Three]
4=[Four, Four]
英文:
Here is one way to do it. I choose a method that handles duplicates by putting them in a list of strings. I added duplicate of each map in the other to demonstrate.
List<Map<String, String>> mapList = new ArrayList<>();
Map<String, String> map1 = new HashMap<>();
map1.put("1", "One");
map1.put("2", "Two");
map1.put("4", "Four");
Map<String, String> map2 = new HashMap<>();
map2.put("3", "Three");
map2.put("1", "One");
map2.put("4", "Four");
mapList.add(map1);
mapList.add(map2);
Map<String,List<String>> combined = mapList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(Entry::getKey,
Collectors.mapping(Entry::getValue,
Collectors.toList())));
combined.entrySet().forEach(System.out::println);
Prints
1=[One, One]
2=[Two]
3=[Three]
4=[Four, Four]
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论