How to Convert Integer List to Int Array in Java
-
Stream().mapToInt()
to Convert Integer List to Int Array in Java -
ArrayUtils.toPrimitive()
to Convert Integer List to Int Array in Java - Guava’s Method to Convert Integer List to Int Array in Java
In this tutorial, we will introduce how we can convert a List<Integer>
to int[]
in Java. We can see that both of them are of different data types that is ArrayList of Integers and the array of int. The former contains an object datatype i.e. Integer, and the latter is a primitive data type i.e. int.
Stream().mapToInt()
to Convert Integer List to Int Array in Java
Stream
is introduced in Java 8 with the package java.util.stream
. It can help us get our desired results because it includes a method mapToInt()
which returns an IntStream
(a sequence of primitive int values).
To make it more clear, let’s see it in the example below.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numList = new ArrayList<Integer>();
numList.add(11);
numList.add(22);
numList.add(33);
numList.add(44);
numList.add(55);
int[] numArray = numList.stream().mapToInt(i -> i).toArray();
for (int intValue : numArray) {
System.out.println(intValue);
}
}
}
Output:
int primitive: 11
int primitive: 22
int primitive: 33
int primitive: 44
int primitive: 55
We can double check the data type of numArray
by debugging. The below debug output shows that the numList is an ArrayList, while numArray
is a primitive int.
numList = {ArrayList @832} size = 5 numArray = {int[5] @833} intValue = 11
ArrayUtils.toPrimitive()
to Convert Integer List to Int Array in Java
We have another way of casting a List<Integer>
to an int[]
type. We will use Apache Common Lang, which is a set of helper methods.
ArrayUtils.toPrimitive()
allows us to pass in our numList
that gives us the result in int[]
datatype.
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
public class Main {
public static void main(String[] args) {
List<Integer> numList = new ArrayList<Integer>();
numList.add(110);
numList.add(220);
numList.add(330);
numList.add(440);
numList.add(550);
int[] intArray = ArrayUtils.toPrimitive(numList.toArray(new Integer[numList.size()]));
for (int intValue : intArray) {
System.out.println("int primitive: " + intValue);
}
}
}
Output:
int primitive: 110
int primitive: 220
int primitive: 330
int primitive: 440
int primitive: 550
Guava’s Method to Convert Integer List to Int Array in Java
Guava
is a library which can help us convert our list of Integers into primitive values of int.
Ints.toArray(ListOfIntegers)
is a part of the com.google.common.primitives.Ints
package that takes our list as an argument and provides us with the array of ints.
import com.google.common.primitives.Ints;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numList = new ArrayList<Integer>();
numList.add(110);
numList.add(220);
numList.add(330);
numList.add(440);
numList.add(550);
int[] numArray = Ints.toArray(numList);
for (int intValue : numArray) {
System.out.println("int primitive: " + intValue);
}
}
}
Output:
int primitive: 110
int primitive: 220
int primitive: 330
int primitive: 440
int primitive: 550
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java Int
- How to Convert Int to Char in Java
- How to Convert Int to Double in Java
- List of Ints in Java
- How to Convert Integer to Int in Java
- How to Check if Int Is Null in Java
- How to Convert Int to Byte in Java
Related Article - Java List
- How to Split a List Into Chunks in Java
- How to Get First Element From List in Java
- How to Find the Index of an Element in a List Using Java
- How to Filter List in Java
- Differences Between List and Arraylist in Java
- List vs. Array in Java