After a lot of searching, I finally managed to write the following methods to box and unbox primitive arrays using lambda expressions (double
specifically):
public static Double[] box(double unboxed[]) {
return Arrays.stream(unboxed).boxed().toArray(Double[]::new);
}
public static double[] unbox(Double boxed[]) {
return Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();
}
But now I needed to do the same with double[][]
and Double[][]
, but I still don't understand lambda expressions enough in order to come up with a solution, neither I found anything searching.
Actually, I ended up needing to wrap my primitive matrices in a class for other reasons, so I no longer need to do this, but I thought it would be useful to have an answer for future reference.
A 2D array is an array of arrays - you can stream it, and then handle each element, which is an array on its own right, individually. A relatively neat way of doing this is to reuse your box
and unbox
methods.
public static Double[][] box(double unboxed[][]) {
return Arrays.stream(unboxed).map(Boxer::box).toArray(Double[][]::new);
}
public static double[][] unbox(Double boxed[][]) {
return Arrays.stream(boxed).map(TmpTest::unbox).toArray(double[][]::new);
}