I'm looking at the docs for the IntStream, and I see an toArray method, but no way to go directly to a List<Integer>
IntStream
toArray
List<Integer>
Surely there is a way to convert a Stream to a List?
Stream
List
IntStream::boxed
IntStream::boxed turns an IntStream into a Stream<Integer>, which you can then collect into a List:
Stream<Integer>
collect
theIntStream.boxed().collect(Collectors.toList())
The boxed method converts the int primitive values of an IntStream into a stream of Integer objects. The word "boxing" names the int ? Integer conversion process. See Oracle Tutorial.
boxed
int
Integer
Java 16 brought the shorter toList method. Produces an unmodifiable list. Discussed here.
toList
theIntStream.boxed().toList()
1.4m articles
1.4m replys
5 comments
56.9k users