Java Convert Wrapper Class Array to Primitive 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 wrapper type objects into an array of primitive values using the ArrayUtils class 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 Wrapper Class Array to Primitive Array in Java

The Apache Commons Lang library provides the method ArrayUtils.toPrimitive() to convert an array of Wrapper type objects into an array of primitive values.

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

ConvertToPrimitiveInteger1.java

import org.apache.commons.lang3.ArrayUtils;

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

        int[] resultArray = ArrayUtils.toPrimitive(arrayToConvert);

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

ConvertToPrimitiveInteger2.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveInteger2 {
    public static void main(String... args) {
        // the Float array has null value
        Integer[] arrayToConvert = new Integer[] { 1234, 2345, 3456, 4567, null, null };

        int[] resultArray = ArrayUtils.toPrimitive(arrayToConvert, -1);

        System.out.println("Output primitive int array: ");
        for(int value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive int array: 
1234
2345
3456
4567
-1
-1

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

ConvertToPrimitiveLong1.java

import org.apache.commons.lang3.ArrayUtils;

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

        long[] resultArray = ArrayUtils.toPrimitive(arrayToConvert);

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

ConvertToPrimitiveLong2.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveLong2 {
    public static void main(String... args) {
        // the Float array has null value
        Long[] arrayToConvert = new Long[] { null, 234567L, 345678L,null, 456789L, null, null };

        long[] resultArray = ArrayUtils.toPrimitive(arrayToConvert, 0L);

        System.out.println("Output primitive long array: ");
        for(long value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive long array: 
0
234567
345678
0
456789
0
0

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

ConvertToPrimitiveDouble1.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveDouble1 {
    public static void main(String... args) {
        Double[] arrayToConvert = new Double[] { 1000.99, 2000.88 ,3000.77, 4000.66 };

        double[] resultArray = ArrayUtils.toPrimitive(arrayToConvert);

        System.out.println("Output primitive double array: ");
        for(double value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive double array: 
1000.99
2000.88
3000.77
4000.66

ConvertToPrimitiveDouble2.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveDouble2 {
    public static void main(String... args) {
        // the Double array has null value
        Double[] arrayToConvert = new Double[] { null, 2000.88 ,null, 4000.66 };

        double[] resultArray = ArrayUtils.toPrimitive(arrayToConvert, 0.0);

        System.out.println("Output primitive double array: ");
        for(double value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive double array: 
0.0
2000.88
0.0
4000.66

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

ConvertToPrimitiveFloat1.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveFloat1 {
    public static void main(String... args) {
        Float[] arrayToConvert = new Float[] { 10.99f, 20.88f ,30.77f, 40.66f };

        float[] resultArray = ArrayUtils.toPrimitive(arrayToConvert);

        System.out.println("Output primitive float array: ");
        for(float value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive float array: 
10.99
20.88
30.77
40.66

ConvertToPrimitiveFloat2.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveFloat2 {
    public static void main(String... args) {
        // the Float array has null value
        Float[] arrayToConvert = new Float[] { 10.99f, 20.88f ,null, 40.66f, null };

        float[] resultArray = ArrayUtils.toPrimitive(arrayToConvert, 0.0f);

        System.out.println("Output primitive float array: ");
        for(float value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive float array: 
10.99
20.88
0.0
40.66
0.0

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

ConvertToPrimitiveBoolean1.java

import org.apache.commons.lang3.ArrayUtils;

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

        boolean[] resultArray = ArrayUtils.toPrimitive(arrayToConvert);

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

ConvertToPrimitiveBoolean2.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveBoolean2 {
    public static void main(String... args) {
        // the Boolean array has null value
        Boolean[] arrayToConvert = new Boolean[] { true, false, null, false, true, null };

        boolean[] resultArray = ArrayUtils.toPrimitive(arrayToConvert, true);

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

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

ConvertToPrimitiveByte1.java

import org.apache.commons.lang3.ArrayUtils;

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

        byte[] resultArray = ArrayUtils.toPrimitive(arrayToConvert);

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

ConvertToPrimitiveByte2.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveByte2 {
    public static void main(String... args) {
        // the Byte array has null value
        Byte[] arrayToConvert = new Byte[] { 10, 20 ,null, 40, null, null };

        byte[] resultArray = ArrayUtils.toPrimitive(arrayToConvert, (byte)0);

        System.out.println("Output primitive byte array: ");
        for(byte value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive byte array: 
10
20
0
40
0
0

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

ConvertToPrimitiveCharacter1.java

import org.apache.commons.lang3.ArrayUtils;

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

        char[] resultArray = ArrayUtils.toPrimitive(arrayToConvert);

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

ConvertToPrimitiveCharacter2.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveCharacter2 {
    public static void main(String... args) {
        // the Character array has null value
        Character[] arrayToConvert = new Character[] { 'a', null, 'c', 'd', null, null };

        char[] resultArray = ArrayUtils.toPrimitive(arrayToConvert, 'z');

        System.out.println("Output primitive char array: ");
        for(char value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive char array: 
a
z
c
d
z
z

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

ConvertToPrimitiveShort1.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveShort1 {
    public static void main(String... args) {
        Short[] arrayToConvert = new Short[] { 100, 200, 300, 400 };

        short[] resultArray = ArrayUtils.toPrimitive(arrayToConvert);

        System.out.println("Output primitive short array: ");
        for(short value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive short array: 
100
200
300
400

ConvertToPrimitiveShort2.java

import org.apache.commons.lang3.ArrayUtils;

public class ConvertToPrimitiveShort2 {
    public static void main(String... args) {
        // the Float array has null value
        Short[] arrayToConvert = new Short[] { 100, null, 300, null, 400 , null, null};

        short[] resultArray = ArrayUtils.toPrimitive(arrayToConvert, (short)0);

        System.out.println("Output primitive short array: ");
        for(short value : resultArray) {
            System.out.println(value);
        }
    }
}
The output is:
Output primitive short array: 
100
0
300
0
400
0
0

Happy Coding 😊