DCR
DCR

Reputation: 15665

Java 8 syntax confusing to understand

Someone replied to a question with an answer I'm having trouble understanding.

the first answer I understand is:

Double[][] inverse = new Double[temp.length][temp[0].length];
for (int i = 0; i < temp.length; i++)
   for (int j = 0; j < temp[0].length; j++)
        inverse[i][j] = temp[i][j];
    }
}

but this answer which requires Java 8 has me confused:

Double[][] inverse = Arrays.stream(temp)
    .map(d -> Arrays.stream(d).boxed().toArray(Double[]::new))
    .toArray(Double[][]::new);

Does Arrays.stream(temp) return each row of temp?

then the next line does the map take each element in the row convert it to an object and place it in a 1d array>

and then the next line takes the 1darray and places it a 2darray?

Upvotes: 1

Views: 152

Answers (1)

azro
azro

Reputation: 54148

This all process take a double[][] and returns a boxed copy, a Double[][]


Does Arrays.stream(temp) return each row of temp ?

Arrays.stream(T[]) return a sequence of the elements contained in the given array. temp is an array of double[] so the stream will be of double[] (so yes "rows" if you see it as a 2D array), in the next operationd will be double[]


Then the next line does the map take each element in the row convert it to an object and place it in a 1d array?

Arrays.stream(double[]) returns a Stream of double values, the ones of the double[] d (local variable). These variables are boxed to Double and put together to make a Double[]


Then the next line takes the 1darray and places it a 2darray?

Then all these Double[] are put together to build a Double[][]

Upvotes: 2

Related Questions