I wrote this code:
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String... args) {
List<String> a = Arrays.asList("One", "Two", "three");
List<Integer> lengths = a.stream().mapToInt(String::length).collect(Collectors.toList());
}
}
but it doesn't want to compile, saying:
Error:(8, 68) java: method collect in interface java.util.stream.IntStream cannot be applied to given types;
required: java.util.function.Supplier<R>,java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>
found: java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.List<java.lang.Object>>
reason: cannot infer type-variable(s) R
(actual and formal argument lists differ in length)
What's happening here? Why the limitation? If you use map. Instead of mapToInt, it works fine.
"The method collect(Supplier<R>, ObjIntConsumer<R>, BiConsumer<R,R>) in the type IntStream is not applicable for the arguments (Collector<Object,?,List<Object>>)"
.