Showing posts with label wrapper data type. Show all posts
Showing posts with label wrapper data type. Show all posts

Saturday, 5 November 2022

Convert a wrapper array to primitive array in Java

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

Interview Questions

Array programs in Java

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

Get reverse iterator for the given array in Java

Convert primitive array to wrapper array in Java

Convert a primitive array to wrapper array in Java

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

Interview Questions

Array programs in Java

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

Get reverse iterator for the given array in Java

How to break out of nested loops in Java?