简体   繁体   中英

Stream to populate 2D array of doubles

I have the following loop that I would like to convert to a parallel stream. Note that mbbVariance and nbbVariance both return a double .

double[][] outMBB = new double[rows][maxBlockSize];
double[][] outNBB = new double[rows][maxBlockSize];

for (int j = 0; j < rows; j++) {
       for (int m = 0; m < maxBlockSize; m++) {
                outMBB[j][m] = mbbVariance(timeSeries[j], m + 1, alpha);
                outNBB[j][m] = nbbVariance(timeSeries[j], m + 1, alpha);
       }
}

My best attempt was

double[][] outMBB = IntStream.range(0, rows)
                                     .parallel()
                                     .mapToDouble(j -> (
                                                IntStream.range(0, maxBlockSize)
                                                         .mapToDouble(m -> mbbVariance(timeSeries[j], m + 1, alpha))
                                                         .toArray(double[]::new)
                                                        )
                                                )
                                      .toArray(double[][]::new);

double[][] outNBB = IntStream.range(0, rows)
                                     .parallel()
                                     .mapToDouble(j -> (
                                                IntStream.range(0, maxBlockSize)
                                                         .mapToDouble(m -> nbbVariance(timeSeries[j], m + 1, alpha))
                                                         .toArray(double[]::new)
                                                        )
                                                )
                                      .toArray(double[][]::new);

but this does not work.

I'm really not a pro with streams, this solution is more of a starting point than a polished product.

double[][] outMBB = new double[rows][maxBlockSize];
double[][] outNBB = new double[rows][maxBlockSize];
IntStream.range(0, rows).forEach(i -> IntStream.range(0, maxBlockSize).forEach(
        m -> {  outFSMBB[j][m] = mbbVariance(timeSeries[j], m + 1, alpha);
                outFSNBB[j][m] = nbbVariance(timeSeries[j], m + 1, alpha);
        }));

This solution is more of a simulation of what you did with loops than a real utilisation of what streams are capable of.

You were really close with your first attempt.

int rows = 4;
int columns = 5;
double[][] outNBB = IntStream.range(0, rows).mapToObj( 
            j -> IntStream.range(0, columns).mapToDouble(
                m -> Math.random()
            ).toArray()
        ).toArray(double[][]::new);

You pretty much just needed to mapToObj and not mapToDouble since a double[][] is actually an array of objects where each object is a double[].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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