第一个声明有效,但第二个声明不正确,为什么?
java.util.Arrays.asList(1,2,3,4,5).stream()
.map(n -> n+1)
.collect(Collectors.toList());
List<Integer> list = IntStream.rangeClosed(1, 10)
.map(n -> n + 1)
.collect(Collectors.toList());
错误:
Type mismatch: cannot convert from Collector<Object,capture#5-of ?,List<Object>>
to Supplier<R>
最佳答案
虽然有一个collect
Stream
上的方法它接受 Collector
, IntStream
上没有这样的方法.
您可以转换您的 IntStream
到 Stream<Integer>
使用 boxed()
方法。
关于java流收集函数给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46418821/