48

Is there a fancy way to cast an Integer array to an int array? (I don't want to iterate over each element; I'm looking for an elegant and quick way to write it)

The other way around I'm using

scaleTests.add(Arrays.stream(data).boxed().toArray(Double[]::new));

I'm looking for an one-liner but wasn't able to find something.

The goal is to:

int[] valuesPrimitives = <somehow cast> Integer[] valuesWrapper
6
  • 1
    The actual question is: why would you want to do that? The damage has already been dealt (you already created all the Double objects and payed with higher execution time and higher memory usage). Commented Jul 13, 2015 at 22:44
  • 2
    No you cannot cast it, as a Double[] is not a double[]. There will be an iteration behind the scenes. A one-liner could be: double[] valuesPrimitives = Stream.of(valuesWrapper).mapToDouble(d -> d).toArray(); Commented Jul 13, 2015 at 22:44
  • 1
    stackoverflow.com/questions/564392/… Commented Jul 13, 2015 at 22:45
  • 1
    @JavaHopper I think you misunderstood me. I see why one wants a short and elegant solution for the conversion. I do not see why one wants the actual conversion. You can simply use the Double[] instead of double[]. Commented Jul 14, 2015 at 6:20
  • 3
    @Turing85 Adapting to foreign APIs is a frequent case. Commented Jul 14, 2015 at 9:27

5 Answers 5

82

You can use Stream APIs of Java 8

int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Sometimes I prefer to see mapToInt(i -> i).
5

If you can consider using Apache commons ArrayUtils then there is a simple toPrimitive API:

public static double[] toPrimitive(Double[] array, double valueForNull)

Converts an array of object Doubles to primitives handling null.

This method returns null for a null input array.

1 Comment

Hi thanks for the answer. Unfortunately I forgot to mention that I cannot use ArrayUtils. But thanks anyways
2

Using Guava, you can do the following:

int[] intArray = Ints.toArray(intList);

If you're using Maven, add this dependency:

<dependency>
   <groudId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>18.0</version>
</dependency>

Comments

1

If you have access to the Apache lang library, then you can use the ArrayUtils.toPrimitive(Integer[]) method like this:

int[] primitiveArray = ArrayUtils.toPrimitive(objectArray);

Comments

1

You can download the org.apache.commons.lang3 jar file which provides ArrayUtils class.
Using the below line of code will solve the problem:

ArrayUtils.toPrimitive(Integer[] nonPrimitive)

Where nonPrimitive is the Integer[] to be converted into the int[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.