Convert int array to Integer array in Java
This post will discuss how to convert primitive integer array to Integer array using plain Java, Guava, and Apache Commons Collections.
The Integer class wraps a value of the primitive int in an object. An object of type Integer contains a single field whose type is int and has several useful methods when dealing with an int.
1. Naive solution
A naive solution is to create an array of Integer type and use a regular for-loop to assign values to it from a primitive integer array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; class Main { // Program to convert primitive integer array to Integer array in Java public static void main(String[] args) { int[] primitiveArray = { 1, 2, 3, 4, 5 }; Integer[] boxedArray = new Integer[primitiveArray.length]; for (int i = 0; i < primitiveArray.length; i++) { boxedArray[i] = Integer.valueOf(primitiveArray[i]); } System.out.println(Arrays.toString(boxedArray)); } } |
Output:
[1, 2, 3, 4, 5]
2. Using Java 8
We can use Java 8 Stream to convert a primitive integer array to Integer array:
- Convert the specified primitive array to a sequential Stream using
Arrays.stream(). - Box each element of the stream to an
IntegerusingIntStream.boxed(). - Return an Integer array containing elements of this stream using
Stream.toArray().
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; class Main { // Program to convert primitive integer array to Integer array in Java 8 and above public static void main(String[] args) { int[] primitiveArray = { 1, 2, 3, 4, 5 }; Integer[] boxedArray = Arrays.stream(primitiveArray) // IntStream .boxed() // Stream<Integer> .toArray(Integer[]::new); System.out.println(Arrays.toString(boxedArray)); } } |
Output:
[1, 2, 3, 4, 5]
We can also use IntStream.of() to get IntStream from integer array.
|
1 2 3 |
Integer[] boxedArray = IntStream.of(primitiveArray) .boxed() .toArray(Integer[]::new); |
3. Using Guava Library
We can also use Guava API to convert a primitive integer array to the Integer array. The idea is to get a fixed-size list using Ints.asList() and call List.toArray() to get an Integer array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import com.google.common.primitives.Ints; import java.util.Arrays; class Main { // Program to convert primitive integer array to Integer array in Java using Guava public static void main(String[] args) { int[] primitiveArray = { 1, 2, 3, 4, 5 }; // v1. Provide runtime type of the specified array, and // Java will itself allocate new array Integer[] boxedArr = Ints.asList(primitiveArray) // List<Integer> .toArray(new Integer[0]); // v2. Allocate a new array with the runtime type of the // specified array and the size of this list Integer[] boxedArray = Ints.asList(primitiveArray) .toArray(new Integer[primitiveArray.length]); System.out.println(Arrays.toString(boxedArray)); } } |
Output:
[1, 2, 3, 4, 5]
4. Using Apache Commons Lang
We can directly use Apache Commons lang’s ArrayUtils.toObject() method to convert an array of primitive ints to objects, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; class Main { // Program to convert primitive integer array to Integer array in Java public static void main(String[] args) { int[] primitiveArray = { 1, 2, 3, 4, 5 }; Integer[] boxedArray = ArrayUtils.toObject(primitiveArray); System.out.println(Arrays.toString(boxedArray)); } } |
Output:
[1, 2, 3, 4, 5]
That’s all about converting int array to Integer array 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 :)