easier work with collections: default Stream.toList?

Ali Lahijani alahijani at gmail.com
Wed May 8 10:45:40 PDT 2013


On 5/8/13, Brian Goetz <brian.goetz at oracle.com> wrote:
> This is something the EG has discussed -- a *small* number of
> convenience methods like toList() which simply expand to
> .collect(toList()).  The biggest challenge here is where to draw the
> line, and every additional one added just creates more fuel for the
> demand to add more, until the API is overwhelmed.  So if we can find a
> stable point with a small number (two or three) such conveniences that
> carry their weight, I wouldn't be averse.

I believe one is an attractive magic number here: only add a
convenience wrapper for toCollection(Supplier) and make sure it is
efficient enough. With this method in place, people can learn basic
stream operations (filter, map) without first learning collectors.
Tutorials can start with

    users = users.stream().filter(p -> p.getPoints() > 50).into(ArrayList::new);

and then go on to teach more advanced stuff like collectors. Having at
least one collector wrapper method in Stream is itself a pedagogical
device for collectors API. When people see how into() is implemented
in Stream, they can use this example to educate themselves about more
collectors and how to find them.

By the way, i do not understand the point in having toList() and
toSet(). If creating spurious empty and singleton collections is a
concern here (which I believe it is), it should also be of concern for
other types of collections. So why not fix the performance of
toCollection() and remove the two special cases?

Best,


More information about the lambda-dev mailing list