Convert Integer[] to int[] array

天涯浪子 提交于 2019-11-27 20:02:59

You can use Stream APIs of Java 8

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

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.

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>

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);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!