Languages
[Edit]
EN

Java - convert List<Double> to double[] - boxed Double list to array of primitive doubles

13 points
Created by:
Kate_C
2892

1. Overview

In this post we cover how to convert List<Double> to double[] in couple of different ways.

Simplest way to do it:

List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
double[] arr = list.stream().mapToDouble(Double::doubleValue).toArray();

System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]

2. Using Java 8 - Stream.mapToDouble example 1

Stream.mapToDouble with method reference.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Example1 {

    public static void main(String[] args) {

        List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
        double[] arr = list.stream().mapToDouble(Double::doubleValue).toArray();

        System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
    }
}

Output:

[1.2, 2.7, 3.9]

3. Using Java 8 - Stream.mapToDouble example 2

Stream.mapToDouble with identity function

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Example2 {

    public static void main(String[] args) {

        List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
        double[] arr = list.stream().mapToDouble(d -> d).toArray();

        System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
    }
}

Output:

[1.2, 2.7, 3.9]

4. Before java 8 - using explicit iteration over list

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Example3 {

    public static void main(String[] args) {

        List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
        double[] arr = new double[list.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = list.get(i).doubleValue();
        }
        System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
    }
}

Output:

[1.2, 2.7, 3.9]

5. Using Guava

import com.google.common.primitives.Doubles;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Example4 {

    public static void main(String[] args) {

        // With Guava
        List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
        double[] arr = Doubles.toArray(list);

        System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
    }
}

Output:

[1.2, 2.7, 3.9]

Maven dependency - Guava version 28.0 

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.0-jre</version>
</dependency>

6. Using Apache commons lang3 

import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Example5 {

    public static void main(String[] args) {

        List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
        Double[] arrBoxed = list.toArray(new Double[list.size()]);
        double[] arr = ArrayUtils.toPrimitive(arrBoxed);

        System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
    }
}

Output:

[1.2, 2.7, 3.9]

Maven dependency - Apache commons lang3 version 3.8.1

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
</dependency>

Merged questions

  1. Java - how to cast List<Double> to double[]
  2. Java - how to convert List<Double> to array of primitive double values
  3. How to convert List of Double objects to array of primitive doubles

Reference

  1. Stream.mapToDouble - Java 8 docs
  2. Doubles.toArray - Guava docs
  3. ArrayUtils.toPrimitive - Apache commons lang3 docs
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.