Stream to populate 2D array of doubles

Clarinetist :

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.

Morinator :

Disclaimer: 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);
        }));

I know that is more of a simulation of what you did with loops than a real utilisation of what streams are capable of.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=389000&siteId=1