收集IntStream进行映射时出错 [英] Error when collecting IntStream to map

查看:128
本文介绍了收集IntStream进行映射时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码

String[] values = ...
.... 
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < values.length; i++) {
    map.put("X" + i, values[i]);
}

由IntelliJ转换为:

is converted by IntelliJ to:

Map<String, Object> map = IntStream.range(0, values.length)
        .collect(Collectors.toMap(
                i -> "X" + i,
                i -> values[i],
                (a, b) -> b));

可以缩写为

Map<String, Object> map = IntStream.range(0, values.length)
            .collect(Collectors.toMap(
                    i -> "X" + i,
                    i -> values[i]));

这两个流版本无法编译.

The 2 stream versions don't compile.

IntelliJ,暗示值i中存在i的问题:

IntelliJ, hints that there is an issue with the i in values[i]:

不兼容的类型.
必需:int
找到:java.lang.Object

Incompatible types.
Required: int
Found: java.lang.Object

编译器抱怨:

错误:(35,17)java:接口java.util.stream.IntStream中的方法collect无法应用于给定类型;
必需:java.util.function.Supplier,java.util.function.ObjIntConsumer,java.util.function.BiConsumer
找到:java.util.stream.Collector>
原因:无法推断类型变量R
(实际和正式论点列表的长度不同)

Error:(35, 17) java: method collect in interface java.util.stream.IntStream cannot be applied to given types;
required: java.util.function.Supplier,java.util.function.ObjIntConsumer,java.util.function.BiConsumer
found: java.util.stream.Collector>
reason: cannot infer type-variable(s) R
(actual and formal argument lists differ in length)

任何人都可以解释原因吗?

Can anyone explain why?

推荐答案

似乎不太确定intelliJ的建议在这里如何工作.只需放一个

Not very certain about how intelliJ's suggestion would work there, it seems inconsistent. Just put a

System.out.print(map);

在声明和循环之间进行

声明,然后它不会建议您进一步用collect代替.

statement between the declaration and loop and then it won't suggest you Replace with collect any further.

使用IntStream#collect时,编译失败是由于

While using the IntStream#collect, the compilation fails for the reason that implementation of collect method expects three specified arguments as visible in the error as well while the

Collectors.toMap(i -> "X" + i, i -> values[i])

仅会导致类型为Collector的单个参数.

would result in only a single argument of type Collector.

将表达式转换为

  • 要么使用forEach

Map<String, Object> map;
IntStream.range(0, values.length).forEach(i -> map.put("X" + i, values[i]));

  • 或使用

  • Or use boxed() to convert the IntStream to Stream<Integer> as:-

    Map<String, Object> map = IntStream.range(0, values.length).boxed()
               .collect(Collectors.toMap(i -> "X" + i, i -> values[i], (a, b) -> b));
    

  • 或者按照@Holger的建议,您可以避免使用forEach和装箱开销,并修改结构以使用IntStream.collect三个参数的变体,如下所示:-

  • Or as suggested by @Holger, you can avoid using forEach and boxing overhead and modify the construct to make use of the IntStream.collect three-arg variant as:-

    Map<String, Object> map = IntStream.range(0, values.length)
               .collect(HashMap::new, (m,i) -> m.put("X"+i,values[i]), Map::putAll);
    

  • 这篇关于收集IntStream进行映射时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆