How do I filter a stream of integers into a list?

Viewed 6973

I am trying to process a stream of Integers and collect the integers that match a predicate (via the compare() function) into a list. Here's a rough outline of the code I've written.

private List<Integer> process() {
    Z z = f(-1);
    return IntStream.range(0, 10)
        .filter(i -> compare(z, f(i)))
        .collect(Collectors.toCollection(ArrayList::new)); // Error on this line
}

private boolean compare(Z z1, Z z2) { ... }
private Z f(int i) { ... }

Unfortunately my solution does not compile and I cannot make sense of the compiler error for the line highlighted:

The method collect(Supplier<R>, ObjIntConsumer<R>, BiConsumer<R,R>) in the type IntStream is not applicable for the arguments (Collector<Object,capture#1-of ?,Collection<Object>>)

Any suggestions?

2 Answers
Related