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?

+57
java list double casting
May 16 '11 at 13:37
source share
7 answers

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.

+40
May 16 '11 at 13:50
source share

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.
+78
Dec 17 '14 at 17:36
source share

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.

+25
May 27 '11 at 3:55
source share

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); 
+6
May 16 '11 at 1:40 pm
source share

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

+5
May 16 '11 at 13:42
source share

According to your question,

 List<Double> frameList = new ArrayList<Double>(); 
  1. You must first convert List<Double> to Double[] using

     Double[] array = frameList.toArray(new Double[frameList.size()]); 
  2. Next, you can convert Double[] to double[] using

     double[] doubleArray = ArrayUtils.toPrimitive(array); 

You can directly use it on one line:

 double[] array = ArrayUtils.toPrimitive(frameList.toArray(new Double[frameList.size()])); 
+4
Feb 15 '17 at 13:21
source share

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.

+2
Mar 27 '16 at 1:20
source share



All Articles