arrays - Java 8 Lambda to convert Number[][] to double[][] -
number[][] intarray = new integer[][]{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; double[][] doublearray = arrays.stream(intarray) .foreach(parray -> arrays.stream(parray) .maptodouble(d ->d.doublevalue()) .toarray()) .toarray();
i want convert number[][] double[][]. above lambda not work, outer toarray not compile.
arrays.stream(intarray) : returns stream of integer[]
foreach : every integer[], creating stream of integers, converting each integer double , returning double[].
the each creates double[] , thought outer toarray return array of double[]
how can work?
here's how it:
double[][] doublearray = arrays.stream(intarray) .map(arr -> stream.of(arr).maptodouble(number::doublevalue).toarray()) .toarray(double[][]::new);
this can decomposed follows:
first use arrays.stream
create stream<number[]>
. each number[]
, create stream<number>
, , use maptodouble
doublestream
, toarray()
double[]
array.
the final toarray
call transforms stream<double[]>
double[][]
array.
Comments
Post a Comment