Convert a double array to Double ArrayList

44,114

Solution 1

Alas, Arrays.asList(..) doesn't work with primitives. Apache commons-lang has

Double[] doubleArray = ArrayUtils.toObject(durationValueArray);
List<Double> list = Arrays.asList(doubleArray);

Solution 2

Using Java 8 Streams API this is achieved with

DoubleStream.of(doublesArray).boxed().collect(Collectors.toList());

If returning an ArrayList as an implementation is required then use

DoubleStream.of(doublesArray).boxed().collect(Collectors.toCollection(ArrayList::new));

This one-liner doesn't require any additional libraries.

Solution 3

Guava's version is even shorter:

List<Double> list = Doubles.asList(doubleArray);

Reference:

Note: This is a varargs method. All varargs methods can be called using an array of the same type (but not of the corresponding boxed / unboxed type!!). These two calls are equivalent:

Doubles.asList(new double[]{1d,2d});
Doubles.asList(1d,2d);

Also, the Guava version doesn't do a full traverse, it's a live List view of the primitive array, converting primitives to Objects only when they are accessed.

Solution 4

Credit to bestsss for the comment which should be the answer:

ArrayList<Double> firstValueList = new ArrayList<Double>();
for(double d : firstValueArray) firstValueList.add(d);

Solution 5

…or with Java 1.7:

double[] firstValueArray = new double[] {1.0, 2.0, 3.0};

ArrayList<Double> list = DoubleStream.of( firstValueArray ).boxed().collect(
    Collectors.toCollection( new Supplier<ArrayList<Double>>() {
      public ArrayList<Double> get() {
        return( new ArrayList<Double>() );
      }
    } ) );
Share:
44,114
gishara
Author by

gishara

Updated on October 10, 2020

Comments

  • gishara
    gishara over 3 years

    When I try to convert a double array to a Double arrayList I got the following error:

    Exception in thread "main" java.lang.ClassCastException: [D cannot be cast to java.lang.Double

    Below is my code.

    double [] firstValueArray ;
    

    ArrayList <Double> firstValueList = new ArrayList (Arrays.asList(firstValueArray));

    I am comparing this list with another list and assign the result to another double variable.

    Please let me know the reason for this error.

  • JAVA
    JAVA over 10 years
    What is varargs method? and how the two calls are equivalent?
  • Sean Patrick Floyd
    Sean Patrick Floyd over 10 years
    @sunny the mechanism is described here: Java Tutorial: Arbitrary Number of Arguments. The key phrase is "It's a shortcut to creating an array manually"
  • Sayan Pal
    Sayan Pal over 7 years
    It seems that the list returned by Doubles.asList(double... backingArray), does not allow any changes, such as adding elements to the list using add or addAll. Updated link to doc: google.github.io/guava/releases/19.0/api/docs/index.html?com‌​/…
  • Sean Patrick Floyd
    Sean Patrick Floyd over 7 years
    @SayanPal yes, by design. that's actually what these Lists have in common with those created by the standard library's Arrays.asList(elements). If you need to add elements, copy the list to an ArrayList or LinkedList: new ArrayList<>(Doubles.asList(1.0d, 2.0d))
  • SpaceTrucker
    SpaceTrucker almost 5 years
    This doesn't seem to be a valid answer for Java 7. java.util.stream.DoubleStream and java.util.function.Supplier were introduced in 1.8.