GuiRitter GuiRitter - 3 years ago 253
Java Question

Java · Box and unbox primitive matrices with lambda

After a lot of searching, I finally managed to write the following methods to box and unbox primitive arrays using lambda expressions (


public static Double[] box(double unboxed[]) {

public static double[] unbox(Double boxed[]) {
return Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();

But now I needed to do the same with
, 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.

Thanks in advance.

Answer Source

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[][]) {

public static double[][] unbox(Double boxed[][]) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download