Convert Object array to Integer array in Java
This post will discuss how to convert the object array to an Integer array in Java.
1. Naive solution
A simple approach is to use a regular for-loop to iterate over the object array, and for every object, we cast it to Integer and assign it to the Integer array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; class Main { // Program to convert the object array to an Integer array in Java public static void main(String[] args) { Object[] objectArray = { 1, 2, 3, 4, 5 }; Integer[] integerArray = new Integer[objectArray.length]; // copy elements from object array to integer array for (int i = 0; i < objectArray.length; i++) { integerArray[i] = (Integer)objectArray[i]; } System.out.println(Arrays.toString(integerArray)); } } |
Output:
[1, 2, 3, 4, 5]
2. Using System.arraycopy() method
We can use System.arraycopy() that copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; class Main { // Program to convert the object array to an Integer array in Java public static void main(String[] args) { Object[] src = { 1, 2, 3, 4, 5 }; Integer[] dest = new Integer[src.length]; System.arraycopy(src, 0, dest, 0, src.length); System.out.println(Arrays.toString(dest)); } } |
Output:
[1, 2, 3, 4, 5]
3. Using Arrays.copyOf() method
We can also use Arrays.copyOf() to copy the specified array to an array of the specified type.
|
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 the object array to an Integer array in Java public static void main(String[] args) { Object[] objectArray = { 1, 2, 3, 4, 5 }; Integer[] integerArray = Arrays.copyOf(objectArray, objectArray.length, Integer[].class); System.out.println(Arrays.toString(integerArray)); } } |
Output:
[1, 2, 3, 4, 5]
4. Using List.toArray() method
Here we first convert the object array to a list of objects and then use the toArray(T[]) method to dump the list into a newly allocated Integer array.
|
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 the object array to an Integer array in Java public static void main(String[] args) { Object[] objectArray = { 1, 2, 3, 4, 5 }; Integer[] integerArray = Arrays.asList(objectArray) .toArray(new Integer[0]); // .toArray(new Integer[objectArray.length]); System.out.println(Arrays.toString(integerArray)); } } |
Output:
[1, 2, 3, 4, 5]
5. Using Java 8
Java 8 provides another simple approach to convert object array to integer array. Following are the steps:
- Convert the specified object array to a sequential Stream.
- Use
Stream.map()to convert every object in the stream to their integer representation. - Use the
toArray()method to accumulate the stream elements into a new integer array.
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 { public static void main(String[] args) { Object[] objectArray = { 1, 2, 3, 4, 5 }; // copy elements from object array to integer array Integer[] integerArray = Arrays.stream(objectArray) .map(o -> (Integer) o) .toArray(Integer[]::new); System.out.println(Arrays.toString(integerArray)); } } |
Output:
[1, 2, 3, 4, 5]
That’s all about converting an Object array to an 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 :)