Convenience Collector forms

Joe Bowbeer joe.bowbeer at gmail.com
Mon Apr 8 19:32:37 PDT 2013


Q: Why is the method on the block called counting() instead of the proposed
count()?

Except for possibly count(), I'm not liking any of these, because:

1. There is already enough exposed "reduce" surface area in max/min/sum.

2. map/reduce is where it's at.  It's easier for me to read code that uses
those familiar forms than it is to familiarize myself with a bunch of new
convenience methods.

I don't think these new forms are going to make Collectors easier to learn,
or collectors code easier to read (except at a very superficial level).



On Mon, Apr 8, 2013 at 5:08 PM, Brian Goetz <brian.goetz at oracle.com> wrote:

> One of the feedback items from the recent London Lambda Hack Day was "more
> convenience forms for Collectors please!".  One suggested was "count()"
> (related are min/max/sum).  Another is a dedicated form for frequency
> counting.
>
> The idea is that:
>  - They are easier to read than their obvious reduce expansion; everyone
> understands count(), even if they don't understand reduce (this was an
> argument in favor of sum() and friends on IntStream).
>  - They provide more on-ramp for understanding reduction and composition
> of reduction; the Javadoc for count() can explain itself in terms of
> reduction, and simple examples like this help connect the dots better.
>  - They are more discoverable that some of the idioms they expand to (once
> someone discovers Collectors.)
>
> The implementations are of course trivial.
>
> So, on the block are:
>
>  - Collector<T,Long> counting()
>  - Collector<T,T>    minBy(Comparator)
>  - Collector<T,T>    maxBy(Comparator)
>  - Collector<T,Long> sumBy(Function<T, Long>)
>  - Collector<T,Map<T,Long>> countingFrequency()
>  - Collector<T,Map<K,Long>> countingFrequency(T -> K classifier)
>
> Q: Other Collector names are all of the form either toXxx or xxxing, which
> read relatively english-like:
>
>   collect(groupingBy(f))
>   collect(toList())
>
> The minBy, maxBy, and sumBy don't follow this form, though still don't
> read terribly.  Sum can easily be "summingBy" but "minningBy" sucks.  Is
> this naming OK?
>
> Q: Do we need separate long and int versions for sumBy()?
>
>


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