Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

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

0 contributions
13 points

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
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more