8

What is the best way to convert Integer array to int array.

The simple solution for it would be :

public int[] toPrimitiveInts(Integer[] ints) {
    int[] primitiveInts = new int[ints.length];
    for(int i = 0; i < ints.length; i++) {
        primitiveInts[i] = ints[i] == null ? 0 : ints[i];
    }
    return primitiveInts;
}

In the above example I taken 0 for null values for the fact that default value for objects/wrappers is null and for int is 0.

This answer shows how to convert int[] to Integer[]

But I don't find an easy way to convert Integer[] to int[].

OhadM
  • 4,687
  • 1
  • 47
  • 57
afzalex
  • 8,598
  • 2
  • 34
  • 61
  • @downvoter Please let me know reason for downvoting so that I could improve my post (or remove it if required) – afzalex Jun 20 '18 at 07:36
  • Related: https://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java?noredirect=1&lq=1 – Cecilya Jun 20 '18 at 07:46
  • I would recommend not replacing the nulls with 0, if this is a general library method, because you can't then tell the difference between `Integer.valueOf(0)` and `null` in the input array. At the very least, making the "null" value an explicit parameter would make it more clear at the call site that some value will be substituted for null. Alternatively, document that nulls should not be passed, and throw an `IllegalArgumentException`. – Andy Turner Jun 20 '18 at 07:47
  • @AndyTurner Agree with you. But cases occur where we need to keep values only in primitive type. – afzalex Jun 20 '18 at 07:49
  • @Cecilya I already visited that answer. But It is not answering my question. Posted the question only when I didn't found any satisfactory answer anywhere on internet. – afzalex Jun 20 '18 at 07:50
  • @afzalex True, but it might be helpful for others - so it's related but not a duplicate. – Cecilya Jun 20 '18 at 07:51

2 Answers2

9

You can use Java 8 Streams:

If the input array has no nulls:

Integer[] integerArr = ...
int[] arr = Arrays.stream(integerArr).mapToInt(Integer::intValue).toArray();

And with handling of nulls:

Integer[] integerArr = ...
int[] arr = Arrays.stream(integerArr).mapToInt(i -> i != null ? i : 0).toArray();
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    Note that this doesn't handle nulls as OP's code would (nor does my answer; but that's by-the-by). – Andy Turner Jun 20 '18 at 07:35
  • 2
    what is `integerArr`? a `List` or should it be `Arrays.stream(integerArr)...`? And what happens with `null` - it seams to be an issue since author checks it in posted code – user85421 Jun 20 '18 at 07:35
  • 1
    You could replace the `mapToInt` with `i -> i != null ? i : 0`. – Andy Turner Jun 20 '18 at 07:37
  • @AndyTurner thanks. I didn't notice the handling of nulls in the question. – Eran Jun 20 '18 at 07:38
  • 1
    @CarlosHeuberger thanks for the comment. Fixed the error – Eran Jun 20 '18 at 07:40
  • 1
    Nifty code using Streams, but ***much* slower**. See [live code at IdeOne.com](https://www.ideone.com/13NZ6x): 684,134 nanos versus 125,094,956 for 713x worse. Or 4,397,424 vs 158,718,022, 13x worse. On my own machine, I am seeing 30 to 40 times longer with streams, using IntelliJ 2018.1.5 with Oracle Java 10.0.1 on macOS Sierra. – Basil Bourque Jun 20 '18 at 08:10
0

There is no way to transform it directly. you will need to traverse the list and unbox every item. Eran's solution is short and leverages java 8 features. if you don't have java 8, then your solution is just as good.

HMM
  • 29
  • 1
  • 10
Adrian B.
  • 1,592
  • 1
  • 20
  • 38