How do I convert Double[] to double[]? How do I convert Double[] to double[]? java java

How do I convert Double[] to double[]?


If you don't mind using a 3rd party library, commons-lang has the ArrayUtils type with various methods for manipulation.

Double[] doubles;...double[] d = ArrayUtils.toPrimitive(doubles);

There is also the complementary method

doubles = ArrayUtils.toObject(d);

Edit: To answer the rest of the question. There will be some overhead to doing this, but unless the array is really big you shouldn't worry about it. Test it first to see if it is a problem before refactoring.

Implementing the method you had actually asked about would give something like this.

double[] getDoubles(int columnIndex) {    return ArrayUtils.toPrimitive(data[columnIndex]);}


In Java 8, this is one-liner:

Double[] boxed = new Double[] { 1.0, 2.0, 3.0 };double[] unboxed = Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();

Note that this still iterates over the original array and creates a new one.


Unfortunately you will need to loop through the entire list and unbox the Double if you want to convert it to a double[].

As far as performance goes, there is some time associated with boxing and unboxing primitives in Java. If the set is small enough, you won't see any performance issues.