How to convert int[] into List<Integer> in Java?

pupeno picture pupeno · Jul 2, 2009 · Viewed 457k times · Source

How do I convert int[] into List<Integer> in Java?

Of course, I'm interested in any other answer than doing it in a loop, item by item. But if there's no other answer, I'll pick that one as the best to show the fact that this functionality is not part of Java.

Answer

mikeyreilly picture mikeyreilly · May 8, 2014

Streams

In Java 8 you can do this

int[] ints = {1,2,3};
List<Integer> list = Arrays.stream(ints).boxed().collect(Collectors.toList());