Question:
How do I convertint[]
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.
Best Answer:
Streams
- In Java 8+ you can make a stream of your
int
array. Call eitherArrays.stream
orIntStream.of
. - Call
IntStream#boxed
to use boxing conversion fromint
primitive toInteger
objects. - Collect into a list using
Stream.collect( Collectors.toList() )
. Or more simply in Java 16+, callStream#toList()
.
Example:
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com
Leave a Review