47

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
el-teedee
  • 1,293
  • 1
  • 15
  • 27
Michael Brenndoerfer
  • 3,483
  • 2
  • 39
  • 50
  • 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). – Turing85 Jul 13 '15 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();` – Alexis C. Jul 13 '15 at 22:44
  • 1
    http://stackoverflow.com/questions/564392/converting-an-array-of-objects-to-an-array-of-their-primitive-types – Andy Turner Jul 13 '15 at 22:45
  • @Turing85, might be he just want to improve the code readability. I, as a developer assume that when I get to see huge code written by him, I can get to business logic quickly, rather than spending time on few lines of code which just does conversion part – JavaHopper Jul 14 '15 at 03:12
  • 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[]`. – Turing85 Jul 14 '15 at 06:20
  • 3
    @Turing85 Adapting to foreign APIs is a frequent case. – Marko Topolnik Jul 14 '15 at 09:27

5 Answers5

78

You can use Stream APIs of Java 8

int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();
Vaibhav
  • 851
  • 6
  • 7
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.

Littm
  • 4,923
  • 4
  • 30
  • 38
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
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>
Janin
  • 292
  • 1
  • 2
  • 7
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);

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[].

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
Shekhar
  • 19
  • 1