如何从流中收集双打列表?

发布于 2025-01-10 07:02:43 字数 1660 浏览 3 评论 0原文

假设我希望从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2025-01-17 07:02:43

使用 .mapToObject() 而不是 .forEach(),因为 forEach 不会返回任何可以进一步处理的内容。

这是我要尝试的代码:

List<double[]> listArray = IntStream.range(0,5)
    .mapToObj(CollectingStreams::methodReturnArray)
    .collect(Collectors.toList());

Instead of the .forEach() use .mapToObject(), as forEach doesn't return anything that could be processed further.

This is the code I would try:

List<double[]> listArray = IntStream.range(0,5)
    .mapToObj(CollectingStreams::methodReturnArray)
    .collect(Collectors.toList());
满意归宿 2025-01-17 07:02:43

您不需要返回数组的方法。你可以这样做。

 List<double[]> listArray= IntStream.range(0,5)
       .mapToObj(i->new double[]{i*1, i*2})
       .toList();                

listArray.forEach(s->System.out.println(Arrays.toString(s)));

印刷

[0.0, 0.0]
[1.0, 2.0]
[2.0, 4.0]
[3.0, 6.0]
[4.0, 8.0]

You don't need a method to return the array. You can do it like this.

 List<double[]> listArray= IntStream.range(0,5)
       .mapToObj(i->new double[]{i*1, i*2})
       .toList();                

listArray.forEach(s->System.out.println(Arrays.toString(s)));

prints

[0.0, 0.0]
[1.0, 2.0]
[2.0, 4.0]
[3.0, 6.0]
[4.0, 8.0]
我一向站在原地 2025-01-17 07:02:43
List<int> numbers = new ArrayList<>();
List<double> numbers.stream().map(number -> (double) number).toList();
List<int> numbers = new ArrayList<>();
List<double> numbers.stream().map(number -> (double) number).toList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文