Java Convert Primitive Array to Wrapper Class Array using Apache Commons Lang

Tags: ArrayUtils Apache Commons Apache Commons Lang array

In this Java tutorial we learn how to convert an array of primitive values into an array of wrapper objects using the ArrayUtils of Apache Commons Lang library.

How to add Apache Commons Lang 3 library to your Java project

To use the Apache Commons Lang 3 library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'org.apache.commons:commons-lang3:3.12.0'

To use the Apache Commons Lang 3 library in the Maven build project, add the following dependency into the pom.xml file.

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

To have more information about the Apache Commons Lang 3 library you can visit the library home page at commons.apache.org/proper/commons-lang/

How to convert Primitive Array to Wrapper Class Array in Java

The Apache Commons Lang library provides the method ArrayUtils.toObject() to convert an array of primitive values into an array of wrapper class objects.

Below code example shows you how to use the ArrayUtils.toObject() to convert an array of int values into an array of Integer objects.

ConvertToObjectInt.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToObjectInt {
    public static void main(String... args) {
        int[] arrayToConvert = new int[] { 1234, 2345, 3456, 4567 };

        Integer[] resultArray = ArrayUtils.toObject(arrayToConvert);

        System.out.println("Output wrapper type Integer array: ");
        for(Integer value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output wrapper type Integer array: 
1234
2345
3456
4567

Below code example shows you how to use the ArrayUtils.toObject() to convert an array of long values into an array of Long objects.

ConvertToObjectLong.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToObjectLong {
    public static void main(String... args) {
        long[] arrayToConvert = new long[] { 123456, 234567, 345678, 456789 };

        Long[] resultArray = ArrayUtils.toObject(arrayToConvert);

        System.out.println("Output wrapper type Long array: ");
        for(Long value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output wrapper type Long array: 
123456
234567
345678
456789

Below code example shows you how to use the ArrayUtils.toObject() to convert an array of double values into an array of Double objects.

ConvertToObjectDouble.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToObjectDouble {
    public static void main(String... args) {
        double[] arrayToConvert = new double[] { 1000.1, 2000.2, 3000.3, 4000.4 };

        Double[] resultArray = ArrayUtils.toObject(arrayToConvert);

        System.out.println("Output wrapper type Double array: ");
        for(Double value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output wrapper type Double array: 
1000.1
2000.2
3000.3
4000.4

Below code example shows you how to use the ArrayUtils.toObject() to convert an array of float values into an array of Float objects.

ConvertToObjectFloat.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToObjectFloat {
    public static void main(String... args) {
        float[] arrayToConvert = new float[] { 10.1f, 20.2f, 30.3f, 40.4f };

        Float[] resultArray = ArrayUtils.toObject(arrayToConvert);

        System.out.println("Output wrapper type Float array: ");
        for(Float value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output wrapper type Float array: 
10.1
20.2
30.3
40.4

Below code example shows you how to use the ArrayUtils.toObject() to convert an array of boolean values into an array of Boolean objects.

ConvertToObjectBoolean.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToObjectBoolean {
    public static void main(String... args) {
        boolean[] arrayToConvert = new boolean[] { true, false, true, true };

        Boolean[] resultArray = ArrayUtils.toObject(arrayToConvert);

        System.out.println("Output wrapper type Boolean array: ");
        for(Boolean value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output wrapper type Boolean array: 
true
false
true
true

Below code example shows you how to use the ArrayUtils.toObject() to convert an array of byte values into an array of Byte objects.

ConvertToObjectByte.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToObjectByte {
    public static void main(String... args) {
        byte[] arrayToConvert = new byte[] { 10, 20, 30 , 40 };

        Byte[] resultArray = ArrayUtils.toObject(arrayToConvert);

        System.out.println("Output wrapper type Byte array: ");
        for(Byte value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output wrapper type Byte array: 
10
20
30
40

Below code example shows you how to use the ArrayUtils.toObject() to convert an array of char values into an array of Character objects.

ConvertToObjectChar.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToObjectChar {
    public static void main(String... args) {
        char[] arrayToConvert = new char[] { 'a', 'b', 'c', 'd' };

        Character[] resultArray = ArrayUtils.toObject(arrayToConvert);

        System.out.println("Output wrapper type Character array: ");
        for(Character value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output wrapper type Character array: 
a
b
c
d

Below code example shows you how to use the ArrayUtils.toObject() to convert an array of short values into an array of Short objects.

ConvertToObjectShort.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToObjectShort {
    public static void main(String... args) {
        short[] arrayToConvert = new short[] { 10, 20, 30, 40 };

        Short[] resultArray = ArrayUtils.toObject(arrayToConvert);

        System.out.println("Output wrapper type Short array: ");
        for(Short value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output wrapper type Short array: 
10
20
30
40

Happy Coding 😊