Convert Integer List to an int array in Java
This post will discuss how to convert a list of Integer to a primitive integer array in Java.
1. Using Java 8
We can use the Stream provided by Java 8 to convert a list of Integer to a primitive integer array in Java. We start by converting given List<Integer> to Stream<Integer> using List.stream() method. Now all we need to do is convert Stream<Integer> to int[]. Please refer to this post for a detailed explanation of it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; class Main { // Program to convert a list of Integer to a primitive integer array in Java public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int[] primitive = list.stream() .mapToInt(Integer::intValue) .toArray(); System.out.println(Arrays.toString(primitive)); } } |
Output:
[1, 2, 3, 4, 5]
How to handle null values in the list?
IntStream.toArray() throws a NullPointerException if any null values are present in the list. There are many options to handle null values:
1. Filter out the null values before mapping:
|
1 2 3 4 |
int[] primitive = list.stream() .filter(Objects::nonNull) .mapToInt(Integer::intValue) .toArray(); |
2. Map the null values to a default value:
|
1 2 3 4 |
int[] primitive = list.stream() .map(i -> (i == null ? 0 : i)) .mapToInt(Integer::intValue) .toArray(); |
3. Handle null inside the lambda expression itself:
|
1 2 3 |
int[] primitive = list.stream() .mapToInt(i -> (i == null ? 0 : i)) .toArray(); |
2. Using Apache Commons Lang
Apache Commons Lang’s ArrayUtils class provides a toPrimitive() method that can convert an Integer array to primitive ints. We need to convert a list of integers to an Integer array first. We can use List.toArray() for easy conversion.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; import java.util.List; class Main { // Program to convert a list of Integer to a primitive integer array in Java public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int[] primitive = ArrayUtils.toPrimitive(list.toArray(new Integer[0])); System.out.println(Arrays.toString(primitive)); } } |
toPrimitive() throws a NullPointerException if any null values are present in the list. We can filter null values before passing them to the function, as shown below:
|
1 2 |
Integer[] boxed = ints.stream().filter(Objects::nonNull).toArray(Integer[]::new); int[] primitive = ArrayUtils.toPrimitive(boxed); |
3. Using Guava Library
Guava’s Ints.toArray() can also convert a list of Integer to a primitive integer array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.primitives.Ints; import java.util.Arrays; import java.util.List; class Main { // Program to convert a list of Integer to a primitive integer array using Guava public static void main(String[] args) { List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5); int[] primitive = Ints.toArray(ints); System.out.println(Arrays.toString(primitive)); } } |
Output:
[1, 2, 3, 4, 5]
Ints.toArray() throws a NullPointerException if any null values are present in the list. We can filter null values before passing it to Ints.toArray() as seen in previous sections.
|
1 2 3 |
int[] primitive = Ints.toArray(ints.stream() .filter(Objects::nonNull) .collect(Collectors.toList())); |
That’s all about converting Integer List to an array of int in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)