Java 8 – 将 List<Map> 转换为单个 Map

huangapple go评论106阅读模式
英文:

Java 8 - List<Map> into Single Map

问题

我有一个 List<Map<String, String>>。我想将它转换为单个映射。

列表的大小可以是1到n。不确定如何在使用Java 8 Stream的情况下执行此操作?

  1. List<Map<String, String>> mapList = new ArrayList<>();
  2. Map<String, String> map1 = new HashMap<>();
  3. map1.put("1", "One");
  4. map1.put("2", "Two");
  5. Map<String, String> map2 = new HashMap<>();
  6. map2.put("3", "Three");
  7. map2.put("4", "Four");
  8. mapList.add(map1);
  9. mapList.add(map2);
  10. Map<String, String> finalMap = new HashMap<>();

在Java 7中,我们可以像这样做:

  1. for (Map<String, String> map : mapList) {
  2. for (String key : map.keySet()) {
  3. finalMap.put(key, map.get(key));
  4. }
  5. }
  6. System.out.println("Final Map " + finalMap); // Final Map {1=One, 2=Two, 3=Three, 4=Four}
英文:

I have a List&lt;Map&lt;String, String&gt;&gt;. 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?

  1. List&lt;Map&lt;String, String&gt;&gt; mapList = new ArrayList&lt;&gt;();
  2. Map&lt;String, String&gt; map1 = new HashMap&lt;&gt;();
  3. map1.put(&quot;1&quot;, &quot;One&quot;);
  4. map1.put(&quot;2&quot;, &quot;Two&quot;);
  5. Map&lt;String, String&gt; map2 = new HashMap&lt;&gt;();
  6. map2.put(&quot;3&quot;, &quot;Three&quot;);
  7. map2.put(&quot;4&quot;, &quot;Four&quot;);
  8. mapList.add(map1);
  9. mapList.add(map2);
  10. Map&lt;String, String&gt; finalMap = new HashMap&lt;&gt;();

We could do something like this in Java 7:

  1. for(Map&lt;String, String&gt; map : mapList) {
  2. for(String key : map.keySet()) {
  3. finalMap.put(key, map.get(key));
  4. }
  5. }
  6. System.out.println(&quot;Final Map &quot; + finalMap); // Final Map {1=One, 2=Two, 3=Three, 4=Four}

答案1

得分: 8

你可以使用 flatMap 然后使用 Collectors.toMap

  1. mapList.stream()
  2. .flatMap(m -> m.entrySet().stream())
  3. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

请注意,重复的键没有被处理。如果要忽略重复键,你可以使用:

  1. Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1)
英文:

You can flatMap it and then use Collectors.toMap:

  1. mapList.stream()
  2. .flatMap(m -&gt; m.entrySet().stream())
  3. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Note, that duplicate keys are not handled. To ignore duplicates, you can use:

  1. Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -&gt; v1)

答案2

得分: 0

这是一种方法来实现。我选择了一种处理重复项的方法,将它们放入字符串列表中。我在其中一个地图中添加了每个地图的重复项,以进行演示。

  1. List<Map<String, String>> mapList = new ArrayList<>();
  2. Map<String, String> map1 = new HashMap<>();
  3. map1.put("1", "One");
  4. map1.put("2", "Two");
  5. map1.put("4", "Four");
  6. Map<String, String> map2 = new HashMap<>();
  7. map2.put("3", "Three");
  8. map2.put("1", "One");
  9. map2.put("4", "Four");
  10. mapList.add(map1);
  11. mapList.add(map2);
  12. Map<String, List<String>> combined = mapList.stream()
  13. .flatMap(m -> m.entrySet().stream())
  14. .collect(Collectors.groupingBy(Entry::getKey,
  15. Collectors.mapping(Entry::getValue,
  16. Collectors.toList())));
  17. combined.entrySet().forEach(System.out::println);

输出结果为

  1. 1=[One, One]
  2. 2=[Two]
  3. 3=[Three]
  4. 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.

  1. List&lt;Map&lt;String, String&gt;&gt; mapList = new ArrayList&lt;&gt;();
  2. Map&lt;String, String&gt; map1 = new HashMap&lt;&gt;();
  3. map1.put(&quot;1&quot;, &quot;One&quot;);
  4. map1.put(&quot;2&quot;, &quot;Two&quot;);
  5. map1.put(&quot;4&quot;, &quot;Four&quot;);
  6. Map&lt;String, String&gt; map2 = new HashMap&lt;&gt;();
  7. map2.put(&quot;3&quot;, &quot;Three&quot;);
  8. map2.put(&quot;1&quot;, &quot;One&quot;);
  9. map2.put(&quot;4&quot;, &quot;Four&quot;);
  10. mapList.add(map1);
  11. mapList.add(map2);
  12. Map&lt;String,List&lt;String&gt;&gt; combined = mapList.stream()
  13. .flatMap(m -&gt; m.entrySet().stream())
  14. .collect(Collectors.groupingBy(Entry::getKey,
  15. Collectors.mapping(Entry::getValue,
  16. Collectors.toList())));
  17. combined.entrySet().forEach(System.out::println);

Prints

  1. 1=[One, One]
  2. 2=[Two]
  3. 3=[Three]
  4. 4=[Four, Four]
  5. </details>

huangapple
  • 本文由 发表于 2020年4月7日 08:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61070979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定