How to make from a list <Double> to double [] in Java?
I have a variable like this:
List<Double> frameList = new ArrayList<Double>(); /* Double elements has added to frameList */ How can I get a new variable of type double[] from this variable in Java with high performance?
High Performance β Each Double object wraps a single Double value. If you want to store all these values ββin a double[] array, you need to iterate over the collection of Double instances. Mapping O(1) not possible, this should be the fastest you can get:
double[] target = new double[doubles.size()]; for (int i = 0; i < target.length; i++) { target[i] = doubles.get(i).doubleValue(); // java 1.4 style // or: target[i] = doubles.get(i); // java 1.5+ style (outboxing) } Thanks for the additional question in the comments;) Here is the source code of the ArrayUtils#toPrimitive fitting method:
public static double[] toPrimitive(Double[] array) { if (array == null) { return null; } else if (array.length == 0) { return EMPTY_DOUBLE_ARRAY; } final double[] result = new double[array.length]; for (int i = 0; i < array.length; i++) { result[i] = array[i].doubleValue(); } return result; } (And believe me, I did not use it for my first answer - although it looks ... pretty similar: -D)
By the way, the complexity of Marcelosβs answer is O (2n), because it iterates twice (behind the scenes): first make double[] from the list, then expand the Double values.
With java-8 , you can do it like this.
double[] arr = frameList.stream().mapToDouble(Double::doubleValue).toArray(); //via method reference double[] arr = frameList.stream().mapToDouble(d -> d).toArray(); //identity function, Java unboxes automatically to get the double value What does he do:
- get
Stream<Double>from the list - match each double instance with its primitive value, as a result we get
DoubleStream - calling
toArray()to get an array.
Guava has a way to do this for you: double [] Doubles.toArray (Collection <Double>)
It will not necessarily be faster than just looping through Collection and adding each Double object to the array, but it is much less for you.
You can use the ArrayUtils class from commons-lang to get double[] from Double[] .
Double[] ds = frameList.toArray(new Double[frameList.size()]); ... double[] d = ArrayUtils.toPrimitive(ds); You can convert to Double[] by calling frameList.toArray(new Double[frameList.size()]) , but you will need to frameList.toArray(new Double[frameList.size()]) over the list / array to convert to Double[]
According to your question,
List<Double> frameList = new ArrayList<Double>(); You must first convert
List<Double>toDouble[]usingDouble[] array = frameList.toArray(new Double[frameList.size()]);Next, you can convert
Double[]todouble[]usingdouble[] doubleArray = ArrayUtils.toPrimitive(array);
You can directly use it on one line:
double[] array = ArrayUtils.toPrimitive(frameList.toArray(new Double[frameList.size()])); You can use primitive collections from Eclipse Collections and generally avoid boxing.
DoubleList frameList = DoubleLists.mutable.empty(); double[] arr = frameList.toArray(); If you cannot or do not want to initialize the DoubleList :
List<Double> frames = new ArrayList<>(); double[] arr = ListAdapter.adapt(frames).asLazy().collectDouble(each -> each).toArray(); Note. I participate in Eclipse collections.