Write a program to convert the primitive array to wrapper array in Java. For example, for the int[] array, you should return Integer[].
Step 1: Let’s define a map, to get the wrapper type equivalent to the given primitive type.
private static final Map<Class<?>, Class<?>> PRIMITIVES_TO_WRAPPERS;
static {
PRIMITIVES_TO_WRAPPERS = new LinkedHashMap<>();
PRIMITIVES_TO_WRAPPERS.put(boolean.class, Boolean.class);
PRIMITIVES_TO_WRAPPERS.put(byte.class, Byte.class);
PRIMITIVES_TO_WRAPPERS.put(char.class, Character.class);
PRIMITIVES_TO_WRAPPERS.put(double.class, Double.class);
PRIMITIVES_TO_WRAPPERS.put(float.class, Float.class);
PRIMITIVES_TO_WRAPPERS.put(int.class, Integer.class);
PRIMITIVES_TO_WRAPPERS.put(long.class, Long.class);
PRIMITIVES_TO_WRAPPERS.put(short.class, Short.class);
PRIMITIVES_TO_WRAPPERS.put(void.class, Void.class);
}
Step 2: Define a method that return a wrapper type equivalent to the given primitive type.
public static Class<?> getWrapperType(Class<?> primitiveType) {
if (primitiveType.isPrimitive()) {
return PRIMITIVES_TO_WRAPPERS.get(primitiveType);
}
throw new IllegalArgumentException("Given type is not primitive");
}
Step 3: Get the component type of given input array.
Class<?> clazz = primitiveArray.getClass(); Class<?> componentType = clazz.getComponentType();
Step 4: Define new array using the input array component type and copy the elements from input array to this wrapper array.
int inputArrayLength = Array.getLength(primitiveArray);
Object[] arr = (Object[]) Array.newInstance(getWrapperType(componentType), inputArrayLength);
for (int i = 0; i < inputArrayLength; i++) {
arr[i] = Array.get(primitiveArray, i);
}
Find the below working application.
ArrayPrimitiveToWrapperDemo.java
package com.sample.app.arrays;
import java.lang.reflect.Array;
import java.util.LinkedHashMap;
import java.util.Map;
public class ArrayPrimitiveToWrapperDemo {
private static final Map<Class<?>, Class<?>> PRIMITIVES_TO_WRAPPERS;
static {
PRIMITIVES_TO_WRAPPERS = new LinkedHashMap<>();
PRIMITIVES_TO_WRAPPERS.put(boolean.class, Boolean.class);
PRIMITIVES_TO_WRAPPERS.put(byte.class, Byte.class);
PRIMITIVES_TO_WRAPPERS.put(char.class, Character.class);
PRIMITIVES_TO_WRAPPERS.put(double.class, Double.class);
PRIMITIVES_TO_WRAPPERS.put(float.class, Float.class);
PRIMITIVES_TO_WRAPPERS.put(int.class, Integer.class);
PRIMITIVES_TO_WRAPPERS.put(long.class, Long.class);
PRIMITIVES_TO_WRAPPERS.put(short.class, Short.class);
PRIMITIVES_TO_WRAPPERS.put(void.class, Void.class);
}
public static Class<?> getWrapperType(Class<?> primitiveType) {
if (primitiveType.isPrimitive()) {
return PRIMITIVES_TO_WRAPPERS.get(primitiveType);
}
throw new IllegalArgumentException("Given type is not primitive");
}
public static Object[] toWrapperArray(final Object primitiveArray) {
if (primitiveArray == null) {
throw new IllegalArgumentException("primitiveArray can't null");
}
final Class<?> clazz = primitiveArray.getClass();
if (!clazz.isArray()) {
throw new IllegalArgumentException("input is not an array");
}
final Class<?> componentType = clazz.getComponentType();
if (!componentType.isPrimitive()) {
throw new IllegalArgumentException("Input is not a primitive array");
}
final int inputArrayLength = Array.getLength(primitiveArray);
final Object[] arr = (Object[]) Array.newInstance(getWrapperType(componentType), inputArrayLength);
for (int i = 0; i < inputArrayLength; i++) {
arr[i] = Array.get(primitiveArray, i);
}
return arr;
}
public static void main(String[] args) {
final int[] primes = new int[] { 2, 3, 5, 7, 11 };
final Integer[] primesWrapperArray = (Integer[]) toWrapperArray(primes);
for (Integer i : primesWrapperArray) {
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
No comments:
Post a Comment