在 Java 8 中使用 Lambda 将流收集到 HashMap 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33606014/
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-08-11 14:49:07  来源:igfitidea点击:

Collect stream into a HashMap with Lambda in Java 8

javalambdahashmap

提问by chao

I have a HashMap which I need to filter using some function:

我有一个 HashMap,我需要使用一些函数对其进行过滤:

HashMap<Set<Integer>, Double> container
Map.Entry<Set<Integer>, Double> map = container.entrySet()
            .stream()
            .filter(k -> k.getKey().size() == size)

For the size = 2 the following should be valid:

对于 size = 2 以下应该是有效的:

containerBeforeFilter = {1,2,3} -> 1.5, {1,2} -> 1.3
containerAfterFilter = {1,2} -> 1.3

After I applied the function in the filter, I want to collect results again into a HashMap. However, when I try to apply the method suggested here, I'm getting illegal statements.

在过滤器中应用该函数后,我想再次将结果收集到 HashMap 中。但是,当我尝试应用此处建议的方法时,却收到了非法语句。

So the following statement, applied after the filter, is illegal:

因此,在过滤器之后应用的以下语句是非法的:

.collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> entry.getValue()));

What would be the proper way of collecting unchanged map values, where the only criteria is satisfying some key?

收集未更改的地图值的正确方法是什么,唯一的标准是满足某个键?

UPDATE

更新

The mistake in the above code is the type of the variable map. It should be Maprather than Map.Entry.

上面代码中的错误是变量的类型map。应该是Map而不是Map.Entry

So the functional code is:

所以功能代码是:

Map<Set<Integer>, Double> map = container.entrySet()
            .stream()
            .filter(k -> k.getKey().size() == size)
            .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));

采纳答案by wero

Seems that Collectors.toMapdoes not pick up the type arguments of stream.collectin your example and only returns a Map<Object,Object>.

似乎Collectors.toMap没有stream.collect在您的示例中选择类型参数,而只返回Map<Object,Object>.

As a workaround you can create the result map yourself and in the last stream step add the filtered entries to the result map:

作为一种解决方法,您可以自己创建结果映射,并在最后一个流步骤中将过滤后的条目添加到结果映射中:

Map<Set<Integer>, Double> result = new HashMap<>();
container.entrySet()
    .stream()
    .filter(entry -> entry.getKey().size() == size)
    .forEach(entry -> result.put(entry.getKey(), entry.getValue()));

回答by provisota

There is a better solution using

有一个更好的解决方案使用

toMap(Function<? super T, ? extends K> keyMapper,
                            Function<? super T, ? extends U> valueMapper,
                            BinaryOperator<U> mergeFunction,
                            Supplier<M> mapSupplier)

You could use it like this:

你可以这样使用它:

HashMap<Set<Integer>, Double> map = container.entrySet()
    .stream()
    .filter(k -> k.getKey().size() == size)
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (prev, next) -> next, HashMap::new));