Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast from List<Double> to double[] in Java?

I have a variable like that:

List<Double> frameList =  new ArrayList<Double>();  /* Double elements has added to frameList */ 

How can I have a new variable has a type of double[] from that variable in Java with high performance?

like image 357
kamaci Avatar asked May 16 '11 13:05

kamaci


People also ask

How do you double a list in Java?

The Doubles. asList() method of Guava's Doubles Class accepts a double array as a parameter and returns a list which has the fixed size. The returned list is backed by the double array which is passed as the argument.

Can you cast a list to an ArrayList in Java?

Convert list To ArrayList In Java. ArrayList implements the List interface. If you want to convert a List to its implementation like ArrayList, then you can do so using the addAll method of the List interface.

What is the difference between double and double in Java?

Double is an object and double is a primitive data type. See this answer for more details. The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.


2 Answers

With java-8, you can do it this way.

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 it does is :

  • get the Stream<Double> from the list
  • map each double instance to its primitive value, resulting in a DoubleStream
  • call toArray() to get the array.
like image 182
Alexis C. Avatar answered Nov 11 '22 19:11

Alexis C.


High performance - every Double object wraps a single double value. If you want to store all these values into a double[] array, then you have to iterate over the collection of Double instances. A O(1) mapping is 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's the sourcecode of the fitting ArrayUtils#toPrimitive 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 trust me, I didn't use it for my first answer - even though it looks ... pretty similiar :-D )

By the way, the complexity of Marcelos answer is O(2n), because it iterates twice (behind the scenes): first to make a Double[] from the list, then to unwrap the double values.

like image 20
Andreas Dolk Avatar answered Nov 11 '22 18:11

Andreas Dolk