Converting an array of objects to an array of their primitive types

JavaArraysPrimitive Types

Java Problem Overview


If you have an array of Java objects which have a primitive type (for example Byte, Integer, Char, etc). Is there a neat way I can convert it into an array of the primitive type? In particular can this be done without having to create a new array and loop through the contents.

So for example, given

Integer[] array

what is the neatest way to convert this into

int[] intArray

Unfortunately, this is something we have to do quite frequently when interfacing between Hibernate and some third party libraries over which we have no control. It seems this would be a quite common operation so I would be surprised if there's no shortcut.

Thanks for your help!

Java Solutions


Solution 1 - Java

Once again, Apache Commons Lang is your friend. They provide ArrayUtils.toPrimitive() which does exactly what you need. You can specify how you want to handle nulls.

Solution 2 - Java

With streams introduced in Java 8 this can be done:

int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();

However, there are currently only primitive streams for int, long and double. If you need to convert to another primitive type such as byte the shortest way without an external library is this:

byte[] byteArray = new byte[array.length];
for(int i = 0; i < array.length; i++) byteArray[i] = array[i];

Or the for loop can be replaced with a stream if you want:

IntStream.range(0, array.length).forEach(i -> byteArray[i] = array[i]);

All of these will throw a NullPointerException if any of your elements are null.

Solution 3 - Java

Unfortunately, there's nothing in the Java platform that does this. Btw, you also need to explicitly handle null elements in the Integer[] array (what int are you going to use for those?).

Solution 4 - Java

Using Guava:

int[] intArray = Ints.toArray(Arrays.asList(array));

Documentation:

Solution 5 - Java

> In particular can this be done without having to create a new array and loop through the contents.

You can't convert an array of Integer to int (i.e. you can't change the type of the elements of an array) in Java. So you either must create a new int[] array and copy the value of the Integer objects into it or you can use an adapter:

class IntAdapter {
    private Integer[] array;
    public IntAdapter (Integer[] array) { this.array = array; }
    public int get (int index) { return array[index].intValue(); }
}

This can make your code a little more readable and the IntAdapter object will only consume a few bytes of memory. The big advantage of an adapter is that you can handle special cases here:

class IntAdapter {
    private Integer[] array;
    public int nullValue = 0;
    public IntAdapter (Integer[] array) { this.array = array; }
    public int get (int index) { 
        return array[index] == null ? nullValue : array[index].intValue();
    }
}

Another solution is to use Commons Primitives which contains lots of predefined adapters. In your case, have a look at ListIntList.

Solution 6 - Java

Or just do it the easy way if you gonna do it only once. But you haven't talked about Integer!=null case.

    //array is the Integer array
    int[] array2 = new int[array.length];
    int i=0;
    for (Integer integer : array) {
        array2[i] = integer.intValue();
        i++;
    }

Solution 7 - Java

using Dollar is simple as:

Integer[] array = ...;
int[] primitiveArray = $(array).toIntArray();

Solution 8 - Java

Here is a generic solution for all primitive types

/**
 * Convert Collection to equivalent array of primitive type
 * @param <S> [in] Object type of source collection
 * @param tcls [in] class of the primitive element
 * @param q [in] source collection
 * @return Equivalent Array of tcls-elements, requires cast to "tcls"[]
 */
public static <S> Object asPrimitiveArray(Class<?> tcls, Collection<S> q)
{
	int n = q.size();
	Object res = Array.newInstance(tcls, n);
	Iterator<S> i = q.iterator();
	int j = 0;
	while (i.hasNext())
	{
		Array.set(res, j++, i.next());
	}
	return res;
}

/**
 * Convert Object array to equivalent array of primitive type
 * @param <S> [in] Object type of source array
 * @param tcls [in] class of the primitive element
 * @param s [in] source array
 * @return Equivalent Array of tcls-elements, requires cast to "tcls"[]
 */
public static <S> Object asPrimitiveArray(Class<?> tcls, S[] s)
{
    return asPrimitiveArray(tcls, Arrays.asList(s));
} 

For Integer to int conversion

Integer[] a = ...
int[] t = (int[]) asPrimitiveArray(int.class, a);

Solution 9 - Java

We can use Stream API to create primitive type arrays from their boxed object counterparts.

For Character[] array conversion to char[], using an accordingly size-allocated custom Collector, with Supplier<CharBuffer>, BiConsumer<CharBuffer, Character> accumulator, BinaryOperator<CharBuffer> combiner and Function<CharBuffer, char[]> finisher), as following will work:

Collector<Character, CharBuffer, char[]> charArrayCollector = Collector.of(
  () -> CharBuffer.allocate(95), 
  CharBuffer::put, 
  CharBuffer::put, 
  CharBuffer::array
);

It supplies the CharBuffer for the printable ASCII chars, accumulating each streamed Character into a CharBuffer instance, combining parallelized processing results from multiple CharBuffer instances in correct order and finally builds the desired char[] array from the accumulated and combined results, after all threads have finished.

First, we're creating a Character[] test array from the standard printable ASCII set, by utilizing the int values from an IntStream, by iterating the ASCII range and mapping each value into a Character Stream, after casting them to char primitives and converting those to Character objects:

Character[] asciiCharacters = IntStream.range(32, 127)
  .mapToObj(i -> Character.valueOf((char)i))
  .toArray(Character[]::new);

Now, we simply need to create a Stream of Characters from the Character array, which then can be collected into a char[] array, by the custom Collector.

char[] asciiChars = Stream.of(asciiCharacters ).collect(charArrayCollector);

This works for other Number types accordingly:

byte[] bytes = new byte[] { Byte.MIN_VALUE, -1 , 0, 1, Byte.MAX_VALUE };
Byte[] boxedBytes = IntStream.range(0, bytes.length)
  .mapToObj(i -> bytes[i])
  .toArray(Byte[]::new);
byte[] collectedBytes = Stream.of(boxedBytes).collect(
  Collector.of(
    () -> ByteBuffer.allocate(boxedBytes.length), 
    ByteBuffer::put, 
    ByteBuffer::put, 
    ByteBuffer::array
  )
);

short[] shorts = new short[] { Short.MIN_VALUE, -1, 0, 1, Short.MAX_VALUE };
Short[] boxedShorts = IntStream.range(0, shorts.length)
  .mapToObj(i -> shorts[i])
  .toArray(Short[]::new);
short[] collectedShorts = Stream.of(boxedShorts).collect(
  Collector.of(
    () -> ShortBuffer.allocate(boxedShorts .length), 
    ShortBuffer::put, 
    ShortBuffer::put, 
    ShortBuffer::array
  )
);

float[] floats = new float[] { Float.MIN_VALUE, -1.0f, 0f, 1.0f, Float.MAX_VALUE };
Float[] boxedFLoats = IntStream.range(0, floats.length)
  .mapToObj(i -> floats[i])
  .toArray(Float[]::new);
float[] collectedFloats = Stream.of(boxedFLoats).collect(
  Collector.of(
    () -> FloatBuffer.allocate(boxedFLoats.length), 
    FloatBuffer::put, 
    FloatBuffer::put, 
    FloatBuffer::array
  )
);

The primitive types, for which Stream API supports it, can be converted a bit easier:

int[] ints = new int[] { Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE };
Integer[] integers = IntStream.of(ints).boxed().toArray(Integer[]::new);
int[] collectedInts = Stream.of(integers).collect(
  Collector.of(
    () -> IntBuffer.allocate(integers.length), 
    IntBuffer::put, 
    IntBuffer::put, 
    IntBuffer::array
  )
);

long[] longs = new long[] { Long.MIN_VALUE, -1l, 0l, 1l, Long.MAX_VALUE };
Long[] boxedLongs = LongStream.of(longs).boxed().toArray(Long[]::new);
long[] collectedLongs = Stream.of(boxedLongs ).collect(
  Collector.of(
    () -> LongBuffer.allocate(boxedLongs .length), 
    LongBuffer::put, 
    LongBuffer::put, 
    LongBuffer::array
  )
);

double[] doubles = new double[] { Double.MIN_VALUE, -1.0, 0, 1.0, Double.MAX_VALUE };
Double[] boxedDoubles = DoubleStream.of(doubles)
  .boxed()
  .toArray(Double[]::new);
double[] collectedDoubles = Stream.of(boxedDoubles).collect(
  Collector.of(
    () -> DoubleBuffer.allocate(boxedDoubles.length), 
    DoubleBuffer::put, 
    DoubleBuffer::put, 
    DoubleBuffer::array
  )
);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIl-BhimaView Question on Stackoverflow
Solution 1 - JavaGuillaumeView Answer on Stackoverflow
Solution 2 - JavaAlex - GlassEditor.comView Answer on Stackoverflow
Solution 3 - JavaZach ScrivenaView Answer on Stackoverflow
Solution 4 - JavaPaul BelloraView Answer on Stackoverflow
Solution 5 - JavaAaron DigullaView Answer on Stackoverflow
Solution 6 - JavaJens JanssonView Answer on Stackoverflow
Solution 7 - JavadfaView Answer on Stackoverflow
Solution 8 - JavaSam GinrichView Answer on Stackoverflow
Solution 9 - JavafozzybearView Answer on Stackoverflow