gpt4 book ai didi

java流收集函数给出错误

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:47 26 4
gpt4 key购买 nike

第一个声明有效,但第二个声明不正确,为什么?

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 上没有这样的方法.

您可以转换您的 IntStreamStream<Integer>使用 boxed()方法。

关于java流收集函数给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46418821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com