Collector / Collectors

Tim Peierls tim at peierls.net
Wed Jun 26 11:42:53 PDT 2013


On Wed, Jun 26, 2013 at 2:26 PM, Brian Goetz <brian.goetz at oracle.com> wrote:

> 2.  Making the one-arg reducing(), and minBy/maxBy, return Optional means
> that queries like "tallest person by city" end up with Optional in the
> value:
>
>   Map<City, Optional<Person>> m
>     = people.collect(groupingBy(**Person::getCity,
>                                 maxBy(comparing(Person::**getHeight)));
>
> Which is doubly bad because the optionals here will *always* be present,
> since otherwise there'd be no associated key.
>
> I can see a few ways to address this:
> [...]
>  - Add a andThen(f) method to Collector.  Then the above would read:
>
>    groupingBy(Person::getCity,
>               maxBy(comparing(Person::**getHeight))
>                 .andThen(Optional::get))
>

Something like this?

interface Collector<T, A, R> {
    /** Returns new collector like this one, but with finisher composed
with this collector's finisher. */
    <R2> Collector<T, A, R2> andThen(Function<? super R, ? extends R2>
finisher);
}

with default implementation doing the obvious composition?

Works for me.

--tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/lambda-libs-spec-experts/attachments/20130626/361860cc/attachment.html 


More information about the lambda-libs-spec-experts mailing list