Learn to convert an array of primitives (int
, long
or double
) to an array of objects (Integer
, Double
or Long
), and vice versa. For example, we will convert int[]
to Integer[]
and then convert back the Integer[]
to int[]
.
Note that all the techniques follow the same logic for the conversion process i.e. iterate over the existing array, convert the current item to object (or primitive) and add to the new array. The APIs discussed below add the syntactic sugar to make the code more readable.
1. Converting using the Stream API
We need to use the Stream.toArray()
method that returns an array containing the items from a given stream. We need to convert the item type before collecting it into the new array.
1.1. Primitive Array to Object Array
Java program to convert the int[] to Integer[]. Note that, by default, toArray()
method returns the Object[] so specifying the generator function Integer[]::new
is important.
int[] primitiveArray = new int[]{0, 1, 2, 3, 4, 5};
Integer[] outputArray = Arrays.stream(primitiveArray)
.boxed()
.toArray(Integer[]::new);
1.2. Object Array to Primitive Array
Java program to convert the Integer[] to int[]. To convert the Integer
object to int
value, we are using the stream.mapToInt()
function. This method returns an IntStream
consisting of the int
values corresponding to the Integer
objects in the array.
If there are null
items in the array then this method will throw NullPointerException.
Integer[] objectArray = new Integer[]{0, 1, 2, 3, 4, 5};
int[] outputArray = Arrays.stream(objectArray)
.mapToInt(Integer::intValue)
.toArray();
2. Converting using Apache Common’s ArrayUtils
The ArrayUtils
class has many static utility methods to perform common tasks in a single statement. It improves the code readability and thus it is easy to maintain such code.
Bothe methods internally use the for-loop for iterating over the original array and populating the new array, so performance-wise you will see much difference as compared to using the Stream API.
Find the latest version of Common’s Lang in the Maven repo.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
2.1. Primitive Array to Object Array
The ArrayUtils.toObject()
method converts an array of primitives to an array of objects. The method returns null
for a null
input array.
int[] primitiveArray = new int[]{0, 1, 2, 3, 4, 5};
Integer[] outputArray = ArrayUtils.toObject(primitiveArray);
2.2. Object Array to Primitive Array
The ArrayUtils.toPrimitive()
method converts an array of objects to an array of corresponding primitives. This method throws NullPointerException if array content is null.
Integer[] objectArray = new Integer[]{0, 1, 2, 3, 4, 5};
int[] outputArray = ArrayUtils.toPrimitive(objectArray);
3. Conclusion
In this simple Java tutorial, we learned to convert primitive arrays to object arrays, and converted object arrays to primitive arrays. We learned to use the Java Streams API and Apache Common’s ArrayUtils
class for it.
Both techniques will
Happy Learning !!
Leave a Reply