List to int[] in Java

How to convert a List<Integer> to a int[] in Java 8? Using Java 8 collections stream() function and then mapping to ints, we get an IntStream. With the IntStream we can call toArray() which gives us the int[]

public int[] toIntArray(List<Integer> list){
   return list.stream().mapToInt(Integer::intValue).toArray();
}

Leave a Reply

Your email address will not be published.

*