如何将 Map> 转换为 Map

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

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

以下是您要的翻译内容:

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

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

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

使用流的版本如下:

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

Here's one way to do it without streams:

Map&lt;String, Integer&gt; resultMap = new LinkedHashMap&lt;&gt;();
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:

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

答案2

得分: 1

这是你的翻译:

这里是你的朋友

    Map<String, Integer> output = new HashMap<String, Integer>();
    Map<String, List<Integer>> gfg = new HashMap<String, List<Integer>>(); 
    for (Map.Entry<String, List<Integer>> entry : gfg.entrySet()) {  
         Integer total = 0;
         for (Integer i : entry.getValue()) {
              total += i;
         }
         output.put(entry.getKey(), Integer.toString(total));
         System.out.println("Key = " + entry.getKey() + 
                           ", Value = " + entry.getValue()); 
    }

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

英文:

Here you go buddy

    Map&lt;String,Integer&gt; output new HashMap&lt;String,Integer&gt;();
    Map&lt;String,List&lt;Integer&gt;&gt; gfg = new HashMap&lt;String,List&lt;Integer&gt;&gt;(); 
    for (Map.Entry&lt;String,List&lt;Integer&gt;&gt; entry : gfg.entrySet()){  
         Integer int = 0;
         for (Integer i :entry.getValue()){
              int+=i
         }
         output.put(entry.getKey(),Integer.toString(int));
         System.out.println(&quot;Key = &quot; + entry.getKey() + 
                           &quot;, Value = &quot; + entry.getValue()); 
    }

This is a more open method for room for minor changes

答案3

得分: 1

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

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

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

Map&lt;String, Integer&gt; resultMap = 
        sourceMap.entrySet()
                 .stream()
                 .collect(Collectors.toMap(e -&gt; e.getKey(), 
                                    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:

确定