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?