如何从流中收集双打列表?
假设我希望从 lambda 函数收集 doubles[] 列表,我创建了以下示例,但是它不会在给定类型的情况下进行编译。我可以采取什么解决方法?
public class CollectingStreams {
public static double [] methodReturnArray(int n){
return new double[] {1*n,2*n};
}
public static void main(String[] args){
List<double[]> listArray= IntStream.range(0,5).parallel().forEach(
(n) -> {methodReturnArray(n).collect(Collectors.toList());
});
}
}
这是编译错误:
java: cannot find symbol
symbol: method collect(java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.List<java.lang.Object>>)
location: class double[]
作为尝试,我尝试了以下方法
List<double[]> listArray= IntStream.range(0,5).parallel().forEach(
(n) -> {
Arrays.stream(methodReturnArray(n)).collect(Collectors.toList());
});
但是它提示了此编译错误:
java: method collect in interface java.util.stream.DoubleStream cannot be applied to given types;
required: java.util.function.Supplier<R>,java.util.function.ObjDoubleConsumer<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)
此外我已经看到(此处)[https://docs.oracle.com/javase/8/docs/api/java/util /stream/package-summary.html] 这五个副作用最好避免使用有利于收集到列表的数组,因此我必须小心使用数组。
我不确定如何正确地转换或修改我的尝试,以便我可以收集我的 double[] 列表。
有什么想法吗?
谢谢
say I desire to collect a list of doubles[] from a lambda function, I have created the following example, however it do not compile given the type. What could I do as a workaround?
public class CollectingStreams {
public static double [] methodReturnArray(int n){
return new double[] {1*n,2*n};
}
public static void main(String[] args){
List<double[]> listArray= IntStream.range(0,5).parallel().forEach(
(n) -> {methodReturnArray(n).collect(Collectors.toList());
});
}
}
Here is the compilation error:
java: cannot find symbol
symbol: method collect(java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.List<java.lang.Object>>)
location: class double[]
As an attempt I have tried the following
List<double[]> listArray= IntStream.range(0,5).parallel().forEach(
(n) -> {
Arrays.stream(methodReturnArray(n)).collect(Collectors.toList());
});
However it prompts this compilation error:
java: method collect in interface java.util.stream.DoubleStream cannot be applied to given types;
required: java.util.function.Supplier<R>,java.util.function.ObjDoubleConsumer<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)
Moreover I have seen (here)[https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html] that fiven side effects it is best to avoid arrays favoring collecting to lists, therefore I must use Arrays with care.
I am not sure exactly how to properly cast or modify my attempts do that I can collect my list of double[].
Any thoughts?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
.mapToObject()
而不是.forEach()
,因为 forEach 不会返回任何可以进一步处理的内容。这是我要尝试的代码:
Instead of the
.forEach()
use.mapToObject()
, as forEach doesn't return anything that could be processed further.This is the code I would try:
您不需要返回数组的方法。你可以这样做。
印刷
You don't need a method to return the array. You can do it like this.
prints