使用 Java 8 Stream API 减少 Map

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39851350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:41:35  来源:igfitidea点击:

Reducing Map by using Java 8 Stream API

javahashmapjava-8java-stream

提问by Fab

I have a Map in the following form:

我有以下形式的地图:

Map<Integer, Map<String,Double>> START

Let INNER be the inner map, i.e.

让 INNER 成为内部映射,即

Map<String,Double>

For example, I'd like to reduce START map in a new one

例如,我想在新的地图中减少 START 地图

Map<Integer, Double> END

which have the same keys, but different values. In particular, for each key, I want the new Double value be the SUM of values in the INNER map for the corresponding key.

它们具有相同的键,但具有不同的值。特别是,对于每个键,我希望新的 Double 值是对应键的 INNER 映射中的值的总和。

How could I achieve this by using JAVA 8's STREAM API?

我如何通过使用 JAVA 8 的 STREAM API 来实现这一点?

Thanks everyone.

感谢大家。

EDIT: A sample map is

编辑:示例地图是

------------------------------
|      |  2016-10-02   3.45   |
| ID1  |  2016-10-03   1.23   |
|      |  2016-10-04   0.98   |
------------------------------
|      |  2016-10-02   1.00   |
| ID2  |  2016-10-03   2.00   |
|      |  2016-10-04   3.00   |
------------------------------

e I'd like a new map like the following one:

e 我想要一张像下面这样的新地图:

--------------------------------
|      |                       |
| ID1  |  SUM(3.45,1.23,0.98)  |
|      |                       |
--------------------------------
|      |                       |
| ID2  |  SUM(1.00,2.00,3.00)  |
|      |                       |
--------------------------------

回答by cringineer

It will work for you

它会为你工作

    Map<Integer, Double> collect = START.entrySet()
        .stream()
        .collect(
            Collectors.toMap(
                Map.Entry::getKey, 
                e -> e.getValue()
                      .values()
                      .stream()
                      .reduce(0d, (a, b) -> a + b)
                )
        );

回答by GROX13

This should be a good example:

这应该是一个很好的例子:

public class Main {

    public static void main(final String[] args) {
        final Map<Integer, Map<String, Double>> tmp = new HashMap<>();
        tmp.put(1, new HashMap<String, Double>() {{
            put("1", 3.45);
            put("2", 1.23);
            put("3", 0.98);
        }});
        tmp.put(2, new HashMap<String, Double>() {{
            put("1", 1.00);
            put("2", 2.00);
            put("3", 3.00);
        }});

        System.out.println(tmp.entrySet().stream()
                .collect(
                    toMap(Map.Entry::getKey, 
                    data -> 
                        data.getValue()
                            .values().stream()
                            .mapToDouble(Number::doubleValue).sum())));
    }

}

output will be {1=5.66, 2=6.0}and all this does is takes entry set of map, gets a stream of it and collects to new map sums of inner map values.

输出将是{1=5.66, 2=6.0},所有这些都是获取映射的条目集,获取它的流并收集内部映射值的新映射总和。