简体   繁体   English

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

[英]How to collect a list of doubles from a stream?

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.假设我想从 lambda function 中收集 doubles[] 的列表,我创建了以下示例,但是在给定类型的情况下它无法编译。 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.此外,我已经看到(这里)[https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html] 最好避免 arrays 有利于收集列表,因此我必须小心使用 Arrays。

I am not sure exactly how to properly cast or modify my attempts do that I can collect my list of double[].我不确定如何正确地投射或修改我的尝试,以便我可以收集我的 double[] 列表。

Any thoughts?有什么想法吗?

Thanks谢谢

Instead of the .forEach() use .mapToObject() , as forEach doesn't return anything that could be processed further.而不是.forEach()使用.mapToObject() ,因为 forEach 不返回任何可以进一步处理的东西。

This is the code I would try:这是我要尝试的代码:

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

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]
List<int> numbers = new ArrayList<>();
List<double> numbers.stream().map(number -> (double) number).toList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM