Write a program to convert the wrapper array to primitive array in Java. For example, for the Integer[] array, you should return int[].
Step 1: Write a function that return a primitive type equivalent to the given wrapper type.
private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE;
static {
WRAPPER_TO_PRIMITIVE = new LinkedHashMap<>();
WRAPPER_TO_PRIMITIVE.put(Byte.class, byte.class);
WRAPPER_TO_PRIMITIVE.put(Short.class, short.class);
WRAPPER_TO_PRIMITIVE.put(Integer.class, int.class);
WRAPPER_TO_PRIMITIVE.put(Long.class, long.class);
WRAPPER_TO_PRIMITIVE.put(Float.class, float.class);
WRAPPER_TO_PRIMITIVE.put(Double.class, double.class);
WRAPPER_TO_PRIMITIVE.put(Boolean.class, boolean.class);
WRAPPER_TO_PRIMITIVE.put(Character.class, char.class);
}
public static Class<?> getPrimitiveType(final Class<?> wrapperType) {
final Class<?> primitiveType = WRAPPER_TO_PRIMITIVE.get(wrapperType);
if (primitiveType == null) {
throw new IllegalArgumentException("Given type is not a wrapper type");
}
return primitiveType;
}
Step 2: Get the component type of wrapper array.
Class<?> wrapperArrayClazz = wrapperArray.getClass(); Class<?> wrapperArrayComponentType = wrapperArrayClazz.getComponentType();
Step 3: Find the equivalent primitive type from the wrapper array component type.
Class<?> primitiveType = getPrimitiveType(wrapperArrayComponentType);
Step 4: Define primitive array and copy the elements one by one.
int length = Array.getLength(wrapperArray);
Object primitiveArray = Array.newInstance(primitiveType, length);
for (int i = 0; i < length; i++) {
Object ele = Array.get(wrapperArray, i);
Array.set(primitiveArray, i, ele);
}
Find the below working application.
ArrayWrapperToPrimitiveDemo.java
package com.sample.app.arrays;
import java.lang.reflect.Array;
import java.util.LinkedHashMap;
import java.util.Map;
public class ArrayWrapperToPrimitiveDemo {
private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE;
static {
WRAPPER_TO_PRIMITIVE = new LinkedHashMap<>();
WRAPPER_TO_PRIMITIVE.put(Byte.class, byte.class);
WRAPPER_TO_PRIMITIVE.put(Short.class, short.class);
WRAPPER_TO_PRIMITIVE.put(Integer.class, int.class);
WRAPPER_TO_PRIMITIVE.put(Long.class, long.class);
WRAPPER_TO_PRIMITIVE.put(Float.class, float.class);
WRAPPER_TO_PRIMITIVE.put(Double.class, double.class);
WRAPPER_TO_PRIMITIVE.put(Boolean.class, boolean.class);
WRAPPER_TO_PRIMITIVE.put(Character.class, char.class);
}
public static Class<?> getPrimitiveType(final Class<?> wrapperType) {
final Class<?> primitiveType = WRAPPER_TO_PRIMITIVE.get(wrapperType);
if (primitiveType == null) {
throw new IllegalArgumentException("Given type is not a wrapper type");
}
return primitiveType;
}
public static Object toPrimitiveArray(final Object wrapperArray) {
if (wrapperArray == null) {
throw new IllegalArgumentException("Wrapper array cannot be null");
}
final Class<?> wrapperArrayClazz = wrapperArray.getClass();
if (!wrapperArrayClazz.isArray()) {
throw new IllegalArgumentException("Given input is not an array");
}
final Class<?> wrapperArrayComponentType = wrapperArrayClazz.getComponentType();
final Class<?> primitiveType = getPrimitiveType(wrapperArrayComponentType);
final int length = Array.getLength(wrapperArray);
final Object primitiveArray = Array.newInstance(primitiveType, length);
for (int i = 0; i < length; i++) {
final Object ele = Array.get(wrapperArray, i);
Array.set(primitiveArray, i, ele);
}
return primitiveArray;
}
public static void main(String[] args) {
Integer[] primes = new Integer[] { 2, 3, 5, 7, 11 };
int[] primesPrimitiveArray = (int[]) toPrimitiveArray(primes);
for (int i : primesPrimitiveArray) {
System.out.println(i);
}
}
}
Output
2 3 5 7 11
You may like
Generic method to concatenate two arrays in Java
Get the string representation of array by given delimiter in Java
Get an iterator from array in Java

