Converting Between Array of Primitives to Array of Objects

Examples of Converting Java primitive arrays to object arrays and vice versa.

Overview

This quick tutorial covers various ways of converting between Java array of primitive types to an array of object types. First, we will understand the concepts of autoboxing and unboxing that converts primitives to objects and objects to primitives. Then, we will use looping or Java streams to do the conversion at the element level. In the, end we will use Apache Commons utility to do these conversions.

Conversion Between Primitives and Wrapper Objects

Java allows automatic conversion between the primitives and their respective wrapper objects through autoboxing and unboxing.

Autoboxing is the automatic conversion of a primitive type to its own wrapper object. For example, we can assign a primitive type to an object type.

double d1 = 2.1; Double[] array = new Double[2]; array[0] = d1;
Code language: Java (java)

Similarly unboxing allows converting wrapper objects into primitives. For example we can create an instance of a wrapper object and covert it to a primitive automatically.

Integer object = Integer.valueOf("2"); int i1 = object;
Code language: Java (java)

However, the autoboxing and unboxing works only when the target type is a primitive or a wrapper object. That is why, when we try to convert an array of primitives to an array of wrapper objects we get compilation error.

double[] primitives = new double[]{1.1, 1.2, 1.3, 1.4}; Double[] objects = primitives; // Compilation error
Code language: Java (java)

In order to covert an array of primitives to array of objects or convert the other way, we need to iterate and use autoboxing or unboxing on each of the array elements.

Convert by Looping

First, we will convert an array of primitive types to an array of wrapper objects. To do that, we will simply iterate through the primitive array and use autoboxing to convert each element to the respective wrapper object type.

double[] primitives = new double[]{1.1, 1.2, 1.3, 1.4}; Double[] objects = new Double[primitives.length]; for (int i = 0; i < primitives.length; i++) { objects[i] = primitives[i]; }
Code language: Java (java)

Similarly, we can use iteration along with unboxing to convert an array of wrapper objects to an array of primitives.

Double[] objects = new Double[]{1.1, 1.2, 1.3, 1.4}; double[] primitives = new double[objects.length]; for (int i = 0; i < objects.length; i++) { primitives[i] = objects[i]; }
Code language: Java (java)

Convert using Streams

Let’s use Java Streams to convert array of primitives to wrapper object array.

To do so, we will create a stream of primitive array elements and use boxed() method to map each primitive element to object type. Lastly, we will use toArray() by providing constructor of Double[] to collect the elements in the form of wrapper object array.

double[] primitives = new double[]{1.1, 1.2, 1.3, 1.4}; Double[] objects = Arrays.stream(primitives) .boxed() .toArray(Double[]::new);
Code language: Java (java)

In order to use streams to convert an array of wrapper objects to primitive array.

Double[] objects = new Double[]{1.1, 1.2, 1.3, 1.4}; double[] primitives = Arrays.stream(objects) .mapToDouble(Double::doubleValue) .toArray();
Code language: Java (java)

As shown, first we created a stream of double objects, and used mapToDouble() method to convert its elements to primitive double type. Lastly, we collected the stream elements to an array of primitives.

Convert using Apache Commons ArrayUtils

So far, the two ways we used involved a few manual steps to perform the conversion. If we wish to avoid that, we can use the ArrayUtils class from Apache Commons library.

<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>{version}</version> </dependency>
Code language: HTML, XML (xml)

Let’s use the Apache Commons library to convert a primitive array to an array of wrapper objects. To do that we will use static method toObject(array) from ArrayUtils.

double[] primitives = new double[]{1.1, 1.2, 1.3, 1.4}; Double[] objects = ArrayUtils.toObject(primitives);
Code language: Java (java)

Similarly to convert the other way we can use static method toPrimitive(array) of the ArrayUtils.

Double[] objects = new Double[]{1.1, 1.2, 1.3, 1.4}; double[] primitives = ArrayUtils.toPrimitive(objects);
Code language: Java (java)

Most importantly, the ArrayUtils#toObject() and ArrayUtils#toPrimitive() methods are not only limited to doubles. The class provides overloaded combinations of both of these methods to support other types – boolean, byte, char, float, int, long, and short.

Summary

In this quick tutorial we studied various ways of converting between an array of primitives and an array of wrapper objects. First, we understood autoboxing and unboxing mechanism and also understood that we cannot use them on the array types. Next, we converted primitive arrays to object arrays and vice versa by using simple loop or Java streams. Lastly, we used utility methods from Apache Commons library to do the conversions.