Convert int[] to Integer[] and vice versa in java

Here is a simple example to convert primitive int array to Integer array and vice versa

Using regular for-loop

package com.programtalk.learn.java.beginner;
//author programtalk.com
public class PrimitiveObjectArrayConversion {

	public static void main(String[] args) {
		
		System.out.println("######### Convert Primitive int[] to Integer[] ##########");
		int[] prmitiveArray = new int[] { 10, 11, 12 };
		Integer[] objectArray = convert2ObjectArray(prmitiveArray);

		System.out.println("Print converted Array");
		for (Integer eachInteger : objectArray) {
			System.out.println(eachInteger.getClass() +" :" +eachInteger);
		}

		System.out.println("\n######### Convert Object Integer[] to Primitive int[]  ##########");
		System.out.println("Print converted Array");
		
		Integer[] obj2 = new Integer[] { 20, 21, 22 };
		int[] newObj2 = convert2PrimitiveArray(obj2);

		for (int eachPrimitiveInt : newObj2) {
			System.out.println("primitive int : " + eachPrimitiveInt);
		}

	}

	public static Integer[] convert2ObjectArray(int[] intArray) {

		Integer[] result = new Integer[intArray.length];
		for (int i = 0; i < intArray.length; i++) {
			result[i] = Integer.valueOf(intArray[i]);
		}
		return result;

	}

	public static int[] convert2PrimitiveArray(Integer[] integerArray) {

		int[] result = new int[integerArray.length];
		for (int i = 0; i < integerArray.length; i++) {
			result[i] = integerArray[i].intValue();
		}
		return result;
	}

}

And the output is :

######### Convert Primitive int[] to Integer[] ##########
Print converted Array
class java.lang.Integer :10
class java.lang.Integer :11
class java.lang.Integer :12

######### Convert Object Integer[] to Primitive int[]  ##########
Print converted Array
primitive int : 20
primitive int : 21
primitive int : 22

Java 8

Using Stream APIs of Java 8.

package com.programtalk.learn.java.beginner;

import java.util.Arrays;
//author programtalk.com
public class PrimitiveObjectArrayConversionJava8 {

	public static void main(String[] args) {
		
		System.out.println("######### Convert Primitive int[] to Integer[] ##########");
		int[] prmitiveArray = new int[] { 10, 11, 12 };
		Integer[] objectArray = convert2ObjectArray(prmitiveArray);

		System.out.println("Print converted Array");
		for (Integer eachInteger : objectArray) {
			System.out.println(eachInteger.getClass() +" :" +eachInteger);
		}

		System.out.println("\n######### Convert Object Integer[] to Primitive int[]  ##########");
		System.out.println("Print converted Array");
		
		Integer[] obj2 = new Integer[] { 20, 21, 22 };
		int[] newObj2 = convert2PrimitiveArray(obj2);

		for (int eachPrimitiveInt : newObj2) {
			System.out.println("primitive int : " + eachPrimitiveInt);
		}

	}

	public static Integer[] convert2ObjectArray(int[] intArray) {
		return Arrays.stream(intArray).boxed().toArray( Integer[]::new );

	}

	public static int[] convert2PrimitiveArray(Integer[] integerArray) {
		return Arrays.stream(integerArray).mapToInt(Integer::intValue).toArray();
	}

}

Commons Arrayutils

If you are not using java 8 and still want to have a oneliner. Use ArrayUtils from commons-lang3.


package com.programtalk.learn.java.beginner;

import org.apache.commons.lang3.ArrayUtils;
//author programtalk.com
public class PrimitiveObjectArrayConversionArrayUtils {

	public static void main(String[] args) {
		
		System.out.println("######### Convert Primitive int[] to Integer[] ##########");
		int[] prmitiveArray = new int[] { 10, 11, 12 };
		Integer[] objectArray = convert2ObjectArray(prmitiveArray);

		System.out.println("Print converted Array");
		for (Integer eachInteger : objectArray) {
			System.out.println(eachInteger.getClass() +" :" +eachInteger);
		}

		System.out.println("\n######### Convert Object Integer[] to Primitive int[]  ##########");
		System.out.println("Print converted Array");
		
		Integer[] obj2 = new Integer[] { 20, 21, 22 };
		int[] newObj2 = convert2PrimitiveArray(obj2);

		for (int eachPrimitiveInt : newObj2) {
			System.out.println("primitive int : " + eachPrimitiveInt);
		}

	}

	public static Integer[] convert2ObjectArray(int[] intArray) {
		return ArrayUtils.toObject(intArray);

	}

	public static int[] convert2PrimitiveArray(Integer[] integerArray) {
		return ArrayUtils.toPrimitive(integerArray);
	}

}

References

Like this post? Don’t forget to share it!

Leave a Reply

Your email address will not be published. Required fields are marked *