Collectors inventory
Remi Forax
forax at univ-mlv.fr
Sat Feb 23 01:40:37 PST 2013
On 02/22/2013 12:01 AM, Brian Goetz wrote:
> As I promised a long time ago, here's an overview of what's in
> Collectors currently.
I think there are too many methods in Collectors, we should restrain
ourselves to 2 forms (3 max).
>
> There are 12 basic forms:
> - toCollection(ctor)
> - toList()
> - toSet()
> - toStringBuilder()
> - toStringJoiner(delimiter)
> - to{Long,Double}Statistics
>
> - groupingBy(classifier, mapFactory, downstream collector)
> - groupingReduce(classifier, mapFactory, mapper, reducer)
> - mapping(mappingFn, downstream collector)
> - joiningWith(mappingFunction, mergeFunction, mapFactory)
> - partitioningBy(predicate, downstream collector)
> - partitioningReduce(predicate, mapper, reducer)
>
> The toXxx forms should be obvious.
>
> Mapping has four versions, analogous to Stream.map:
> - mapping(T -> U, Collector<U, R>)
> - mapping(T -> int, Collector.OfInt<R>)
> - mapping(T -> long, Collector.OfLong<R>)
> - mapping(T -> double, Collector.OfDouble<R>)
>
> GroupingBy has four forms:
> - groupingBy(T->K) -- standard groupBy, values of resulting Map are
> Collection<T>
> - Same, but with explicit constructors for map and for rows (so you
> can produce, say, a TreeMap<K, TreeSet<T>> and not just a
> Map<K,Collection<T>>)
> - groupingBy(T->K, Collector<T,D>) -- multi-level groupBy, where
> downstream is another Collector
> - Same, but with explicit ctor for map
You can remove the third one give, you have the one with an explicit
constructor.
>
> GroupingReduce has four forms:
> - groupingReduce(T->K, BinaryOperator<T>) // simple reduce
> - groupingReduce(T->K, Function<T,U>, BinaryOperator<U>) // map-reduce
> - above two with explicit map ctors
keep only the ones with explicit constructors.
>
> JoiningWith has four forms:
> - joiningWith(T->U)
> - same, but with explicit Map ctor
> - same, but with merge function for handling duplicates
> - same, with both explicit map ctor and merge function
remove the third one.
>
> PartitioningBy has three forms:
> - partitioningBy(Predicate)
> - Same, but with explicit constructor for Collection (so you can get
> a Map<Boolean, TreeSet<T>>)
> - partitioningBy(Predicate, Collector) // multi-level
>
> PartitioningReduce has two forms:
> - predicate + reducer
> - predicate + mapper + reducer
>
> Impl note: in any category, all but one are one-liners that delegate
> to the general form.
Rémi
More information about the lambda-libs-spec-experts
mailing list