Arrays.stream(intArray) returns an IntStream - a sequence of primitive int-valued elements. Now if we check javadocs for IntStream we can see that there isn't method collect which accepts an instance of Collector. On the contrary there is the following method:
IntStream#collect(Supplier,ObjIntConsumer,BiConsumer)
Performs a mutable reduction operation on the elements of this stream.
You still can transform your IntStream into a List without using boxed.
List<Integer> list = Arrays.stream(intArray)
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
It works in the following way:
ArrayList::new is a Supplier - a function that creates a new result container, in our case a new ArrayList.
ArrayList::add is a ObjIntConsumer - a function which adds an additional element into a result container.
ArrayList::addAll is a BiConsumer - a function for combining two values. It is used when stream is executed in parallel to combine two partial results. In this case if stream produces 2 ArrayList we need to combine them into one and proceed to the next stage.
All these 3 elements could be abstracted into one single call using collect(Collectors.toList()). But in order to use a Collector an primitive stream IntStream must be converted to more general Stream<Integer>. This could be done using boxed() method which just performs this conversion.
I hope this helps.
.collect(Collectors.toList())does not necessarily return anArrayList; it could be some other kind of list. I presume you specifically want an ArrayList because you create one in your first code snippet.