Convenience Collector forms

Brian Goetz brian.goetz at oracle.com
Mon Apr 8 17:08:02 PDT 2013


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