如何将 Map> 转换为 Map

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

How to change Map<String, List<Integer>> to Map<String, Integer>

问题

我已经创建了一个 Map<String, List<Integer>>,用于存储一个键对应的多个值。现在我想对 List 中的所有值求和,并将类型更改为 Map<String, Integer>。请问如何在 Java 8 的流式处理中实现这个,是否可能在没有流式处理的情况下达到相同的效果呢?

英文:

I have created a Map&lt;String, List&lt;Integer&gt;&gt; to store multiple values against a key. Now I want to sum all those values in List<Integer> and change the type to Map&lt;String, Integer&gt;. How can I do this with Java 8 streams & Is it possible to do the same without it

答案1

得分: 3

以下是您要的翻译内容:

这是一种在没有使用流的情况下执行的方法:

  1. Map<String, Integer> resultMap = new LinkedHashMap<>();
  2. sourceMap.forEach((k, v) -> v.forEach(e -> resultMap.merge(k, e, Integer::sum)));

这使用了 Map.merge 方法来执行归约操作。

使用流的版本如下:

  1. Map<String, Integer> resultMap = sourceMap.entrySet().stream()
  2. .flatMap(e -> e.getValue().stream()
  3. .map(v -> new AbstractMap.SimpleEntry<>(e.getKey(), v)))
  4. .collect(Collectors.toMap(
  5. e -> e.getKey(),
  6. e -> e.getValue(),
  7. Integer::sum));
英文:

Here's one way to do it without streams:

  1. Map&lt;String, Integer&gt; resultMap = new LinkedHashMap&lt;&gt;();
  2. sourceMap.forEach((k, v) -&gt; v.forEach(e -&gt; resultMap.merge(k, e, Integer::sum)));

This uses the Map.merge method to perform the reduction.

The version with streams would be as follows:

  1. Map&lt;String, Integer&gt; resultMap = sourceMap.entrySet().stream()
  2. .flatMap(e -&gt; e.getValue().stream()
  3. .map(v -&gt; new AbstractMap.SimpleEntry&lt;&gt;(e.getKey(), v)))
  4. .collect(Collectors.toMap(
  5. e -&gt; e.getKey(),
  6. e -&gt; e.getValue(),
  7. Integer::sum));

答案2

得分: 1

这是你的翻译:

这里是你的朋友

  1. Map<String, Integer> output = new HashMap<String, Integer>();
  2. Map<String, List<Integer>> gfg = new HashMap<String, List<Integer>>();
  3. for (Map.Entry<String, List<Integer>> entry : gfg.entrySet()) {
  4. Integer total = 0;
  5. for (Integer i : entry.getValue()) {
  6. total += i;
  7. }
  8. output.put(entry.getKey(), Integer.toString(total));
  9. System.out.println("Key = " + entry.getKey() +
  10. ", Value = " + entry.getValue());
  11. }

这是一个更加开放的方法,可以进行细微的更改。

英文:

Here you go buddy

  1. Map&lt;String,Integer&gt; output new HashMap&lt;String,Integer&gt;();
  2. Map&lt;String,List&lt;Integer&gt;&gt; gfg = new HashMap&lt;String,List&lt;Integer&gt;&gt;();
  3. for (Map.Entry&lt;String,List&lt;Integer&gt;&gt; entry : gfg.entrySet()){
  4. Integer int = 0;
  5. for (Integer i :entry.getValue()){
  6. int+=i
  7. }
  8. output.put(entry.getKey(),Integer.toString(int));
  9. System.out.println(&quot;Key = &quot; + entry.getKey() +
  10. &quot;, Value = &quot; + entry.getValue());
  11. }

This is a more open method for room for minor changes

答案3

得分: 1

@fps已经发布了一个很好的答案,你可以尝试使用流来实现这个简单的方法。

  1. Map<String, Integer> resultMap =
  2. sourceMap.entrySet()
  3. .stream()
  4. .collect(Collectors.toMap(e -> e.getKey(),
  5. e -> e.getValue().stream().mapToInt(i -> i).sum()));
英文:

@fps already post a good answer, You can try this simple way using stream

  1. Map&lt;String, Integer&gt; resultMap =
  2. sourceMap.entrySet()
  3. .stream()
  4. .collect(Collectors.toMap(e -&gt; e.getKey(),
  5. e -&gt; e.getValue().stream().mapToInt(i -&gt; i).sum()));

huangapple
  • 本文由 发表于 2020年10月2日 00:41:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64159787.html
匿名

发表评论

匿名网友

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

确定