Error when collecting IntStream to map

柔情痞子 提交于 2020-12-26 06:32:06

问题


The following code

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

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));

which can be shortened to

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, hints that there is an issue with the i in values[i]:

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

The compiler complains:

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?


回答1:


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

System.out.print(map);

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


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])

would result in only a single argument of type Collector.


Better way to convert the expression would be though to

  • either use 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));
    
  • 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);
    


来源:https://stackoverflow.com/questions/46667931/error-when-collecting-intstream-to-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!