Arrays.stream(new int[] {1,2,3,4,5,6})
creates an IntStream
, which doesn't have a collect
method taking a single parameter (collect
method of IntStream
has the signature - <R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R, R> combiner)
). Even if it did, toList()
wouldn't be applicable, since Java doesn't allow List<int>
(i.e. List
s with primitive elements). The elements of a List
must be of reference type.
You can work with the wrapper Integer
type instead:
var list = Arrays.stream(new Integer[] {1,2,3,4,5,6})
.filter(x -> x > 3)
.collect(toList());
Or keep working with an IntStream
, and box it to a Stream<Integer>
later in order to collect the elements to a List<Integer>
:
var list = Arrays.stream(new int[] {1,2,3,4,5,6})
.filter(x -> x > 3)
.boxed()
.collect(toList());
If you wish to to keep working with int
s, you can produce an int
array from the elements of the filtered IntStream
:
var array = Arrays.stream(new int[] {1,2,3,4,5,6})
.filter(x -> x > 3)
.toArray();