Design for collections upgrades

Lawrence Kesteloot lk at teamten.com
Tue Mar 8 13:37:37 PST 2011


On Tue, Mar 8, 2011 at 1:24 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> This can be replaced by the one liner:
>
> ArrayList<T> filtered = otherList.filter(predicate);

This isn't a very strong argument, since the following is more clear
and flexible:

ArrayList<T> filtered = new ArrayList(otherList.filter(predicate));

(Assuming that filter() returns a Stream and that an ArrayList
constructor takes one. This also answers Remi's question about
LinkedList.)

A better argument against making filter() return a stream is that it's
unexpected. The verb "filter" means "filter", not "create a stream
that when later evaluated will filter." You can imagine users putting
logging statements in their predicates, calling:

    otherList.filter(predicate)

and not seeing anything logged. Or programmers repeatedly evaluating
the contents of the stream, not realizing that they're calling the
predicate every time. I don't feel strongly that we must have an eager
API, but I do feel like we can't have a streaming API that looks like
an eager one.

Lawrence


More information about the lambda-dev mailing list