LS Price Increase Launch

The Price of all “Learn Spring” course packages will increase by $40 on next Friday:

>> GET ACCESS NOW

1. Introduction

In this short tutorial, we'll show how to convert an array of primitives to an array of objects, and vice versa.

2. Problem

Let's say we have an array of primitives, such as int[], and we would like to convert it to an array of objects, Integer[]. We might intuitively try casting:

Integer[] integers = (Integer[])(new int[]{0,1,2,3,4});

However, this will result in a compilation error because of inconvertible types. That's because autoboxing only applies to individual elements and not to arrays or collections.

Therefore, we need to convert the elements one by one. Let's take a look at a couple of options to do that.

3. Iteration

Let's see how we can use autoboxing in an iteration.

First, let's convert from a primitive array to an object array:

int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };

Integer[] output = new Integer[input.length];
for (int i = 0; i < input.length; i++) {
    output[i] = input[i];
}

assertArrayEquals(expected, output);

Now, let's convert from an array of objects to an array of primitives:

Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };

int[] output = new int[input.length];
for (int i = 0; i < input.length; i++) {
    output[i] = input[i];
}

assertArrayEquals(expected, output);

As we can see, this is not complicated at all, but a more readable solution, like the Stream API, might suit our needs better.

4. Streams

Since Java 8, we can use the Stream API to write fluent code.

First, let's see how to box the elements of a primitive array:

int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };

Integer[] output = Arrays.stream(input)
  .boxed()
  .toArray(Integer[]::new);

assertArrayEquals(expected, output);

Notice the Integer[]::new parameter in the toArray method. Without this parameter, the stream would return an Object[] instead of the Integer[].

Next, to convert them back, we'll use the mapToInt method together with the unboxing method of Integer:

Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };

int[] output = Arrays.stream(input)
  .mapToInt(Integer::intValue)
  .toArray();

assertArrayEquals(expected, output);

With the Stream API, we created a more readable solution, but if we still wish it were more concise, we could try a library, like Apache Commons.

5. Apache Commons

First, let's add the Apache Commons Lang library as a dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

Then, to convert a primitives array to its boxed counterpart, let's use the ArrayUtils.toObject method:

int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };

Integer[] output = ArrayUtils.toObject(input);

assertArrayEquals(expected, output);

Lastly, to convert back the boxed elements to primitives, let's use the ArrayUtils.toPrimitives method:

Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };

int[] output = ArrayUtils.toPrimitive(input);

assertArrayEquals(expected, output);

The Apache Commons Lang library provides a concise, easy-to-use solution to our problem, with the cost of having to add a dependency.

6. Conclusion

In this article, we've looked at a couple of ways to convert an array of primitives to an array of their boxed counterparts, and then, convert the boxed elements back to their primitive counterparts.

As always, the code examples of this article are available over on GitHub.

LS Price Increase Launch

The Price of all “Learn Spring” course packages will increase by $40 on next Friday:

>> GET ACCESS NOW
Generic footer banner
Comments are closed on this article!