Converting Between Array of Primitives to Array of Objects

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

Overview

In this tutorial, we’ll explore various techniques for converting Java arrays of primitive types to arrays of their respective wrapper object types, and vice versa. We’ll start by understanding the foundational concepts of autoboxing and unboxing, which facilitate conversions between primitives and wrapper objects. Then, we’ll delve into different methods to handle array-level conversions, including looping, Java Streams, and the Apache Commons utility library.

Understanding Autoboxing and Unboxing

Java simplifies conversions between primitives and their corresponding wrapper objects through autoboxing and unboxing:

Autoboxing automatically converts a primitive to its wrapper object.

double d1 = 2.1;
Double wrapper = d1; // AutoboxingCode language: Java (java)

Unboxing performs the reverse, converting a wrapper object back to a primitive.

Integer wrapper = Integer.valueOf("2");
int primitive = wrapper; // UnboxingCode language: Java (java)

However, this mechanism doesn’t extend to arrays. Directly assigning a primitive array to a wrapper object array leads to a compilation error:

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

To handle arrays, we need to convert each element individually, either manually or using helper methods.

Convert by Looping

From Primitive Array to Wrapper Object Array
Iterate through the primitive array and use autoboxing for each element:

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)

From Wrapper Object Array to Primitive Array
Similarly, use iteration and unboxing for each element of array:

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.

From Primitive Array to Wrapper Object Array
With Java Streams, the process becomes more concise:

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)

From Wrapper Object Array to Primitive Array
Stream the wrapper object array and map each element to its primitive equivalent:

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)

Convert using Apache Commons ArrayUtils

The Apache Commons Lang library provides a utility class, ArrayUtils, that simplifies these conversions:

  1. Add the dependency:


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

2. Use the static methods toObject() and toPrimitive():

Use toObject() method to covert a primitive array to an array of wrapper objects and use toPrimitive() method to covert an array of wrapper objects to a primitive array.

double[] primitives = {1.1, 1.2, 1.3, 1.4};
Double[] objects = ArrayUtils.toObject(primitives);

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

The ArrayUtils methods work seamlessly with other primitive types like int, boolean, float, long, and more.

Comparison of Methods

MethodEase of UsePerformanceFlexibility
LoopingModerateHighSupports all types
Java StreamsHighModerateRequires Java 8+
Apache CommonsVery HighHighRequires external lib

Summary

In this guide, we’ve explored multiple approaches to convert between Java primitive arrays and wrapper object arrays:

  1. Leveraged autoboxing and unboxing for element-level conversions.
  2. Used looping and streams for manual conversions.
  3. Streamlined the process with Apache Commons ArrayUtils.

Choose the method that best fits your project’s requirements in terms of performance, simplicity, and dependencies.