Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when collecting IntStream to map

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?

like image 205
msayag Avatar asked Oct 10 '17 13:10

msayag


1 Answers

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);
    
like image 147
Naman Avatar answered Sep 27 '22 18:09

Naman