DE
Java - List<Double> in double[] - boxed Double list in Array primitiver doubles konvertieren
4 points
In diesem Artikel wird gezeigt, wie kann man List<Double> in double[] auf ein paar verschiedenen Arten konvertieren.
Einfachste Möglichkeit:
xxxxxxxxxx
1
List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
2
double[] arr = list.stream().mapToDouble(Double::doubleValue).toArray();
3
4
System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
Stream.mapToDouble mit Referenzmethode.
xxxxxxxxxx
1
import java.util.ArrayList;
2
import java.util.Arrays;
3
import java.util.List;
4
5
public class Example1 {
6
7
public static void main(String[] args) {
8
9
List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
10
double[] arr = list.stream().mapToDouble(Double::doubleValue).toArray();
11
12
System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
13
}
14
}
Ausgabe:
xxxxxxxxxx
1
[1.2, 2.7, 3.9]
Stream.mapToDouble mit Identitätsfunktion.
xxxxxxxxxx
1
import java.util.ArrayList;
2
import java.util.Arrays;
3
import java.util.List;
4
5
public class Example2 {
6
7
public static void main(String[] args) {
8
9
List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
10
double[] arr = list.stream().mapToDouble(d -> d).toArray();
11
12
System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
13
}
14
}
Ausgabe:
xxxxxxxxxx
1
[1.2, 2.7, 3.9]
xxxxxxxxxx
1
import java.util.ArrayList;
2
import java.util.Arrays;
3
import java.util.List;
4
5
public class Example3 {
6
7
public static void main(String[] args) {
8
9
List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
10
double[] arr = new double[list.size()];
11
for (int i = 0; i < arr.length; i++) {
12
arr[i] = list.get(i).doubleValue();
13
}
14
System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
15
}
16
}
Ausgabe:
xxxxxxxxxx
1
[1.2, 2.7, 3.9]
xxxxxxxxxx
1
import com.google.common.primitives.Doubles;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.List;
6
7
public class Example4 {
8
9
public static void main(String[] args) {
10
11
// With Guava
12
List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
13
double[] arr = Doubles.toArray(list);
14
15
System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
16
}
17
}
Ausgabe:
xxxxxxxxxx
1
[1.2, 2.7, 3.9]
Maven-Abhängigkeit - Guava Version 28.0
xxxxxxxxxx
1
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
2
<dependency>
3
<groupId>com.google.guava</groupId>
4
<artifactId>guava</artifactId>
5
<version>28.0-jre</version>
6
</dependency>
7
xxxxxxxxxx
1
import org.apache.commons.lang3.ArrayUtils;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.List;
6
7
public class Example5 {
8
9
public static void main(String[] args) {
10
11
List<Double> list = new ArrayList<>(Arrays.asList(1.2, 2.7, 3.9));
12
Double[] arrBoxed = list.toArray(new Double[list.size()]);
13
double[] arr = ArrayUtils.toPrimitive(arrBoxed);
14
15
System.out.println(Arrays.toString(arr)); // [1.2, 2.7, 3.9]
16
}
17
}
Ausgabe:
xxxxxxxxxx
1
[1.2, 2.7, 3.9]
Maven-Abhängigkeit - Apache commons lang3 Version 3.8.1
xxxxxxxxxx
1
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
2
<dependency>
3
<groupId>org.apache.commons</groupId>
4
<artifactId>commons-lang3</artifactId>
5
<version>3.8.1</version>
6
</dependency>