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.
Here is a generic way to convert array to ArrayList
Usage
Also from guava libraries... com.google.common.primitives.Ints:
I'll add another answer with a different method; no loop but an anonymous class that will utilize the autoboxing features:
In Java 8 with stream:
or with Collectors
If you're open to using a third party library, this will work in Eclipse Collections:
Note: I am a committer for Eclipse Collections.
Here is a solution:
Output: