paul.sandoz at oracle.com
Tue Jul 23 02:35:58 PDT 2013
On Jul 21, 2013, at 8:19 PM, Richard Warburton <richard.warburton at gmail.com> wrote:
> partitioningBy and groupingBy both have a variant with a default
> downstream collector, eg:
> public static <T, K> Collector<T, ?, Map<K, List<T>>>
> groupingBy(Function<? super T, ? extends K> classifier)
> Is there any reason why there is no such equivalent for mapping?
Some reasons IIRC:
- simply to reduce the number of methods.
- the "telescoping" is applied to collectors more likely to be used at the top-level returning a Map where List is the value type, whereas mapping is more likely to be used at a sub-level, since a map(mapper).collect(downstream) could, and preferably should, be used instead.
- if mapping has a default toList downstream collector then map(mapper).collect(toList()) and collect(mapping(mapper)) do the same thing.
Personally i prefer mapping as is so as not to encourage top-level usage at the expense of being slightly more verbose in multi-level case.
> It would
> seem to be a fairly common use case (at least from my Collector usage).
> Especially given that I seem to be using mapping as a downstream collector
> for groupingBy or partitioningBy rather than anything else. In which case
> why not have the 'leaf' collection default be the same?
> Dr. Richard Warburton
> @RichardWarburto <http://twitter.com/richardwarburto>
More information about the lambda-dev