Summary of Static Factory Methods in the Collectors Class – Streams

Summary of Static Factory Methods in the Collectors Class

The static factory methods of the Collectors class that create collectors are summarized in Table 16.7. All methods are static generic methods, except for the overloaded joining() methods that are not generic. The keyword static is omitted, as are the type parameters of a generic method, since these type parameters are evident from the declaration of the formal parameters to the method. The type parameter declarations have also been simplified, where any bound <? super T> or <? extends T> has been replaced by <T>, without impacting the intent of a method. A reference is also provided for each method in the first column.

The last column in Table 16.7 indicates the function type of the corresponding parameter in the previous column. It is instructive to note how the functional interface parameters provide the parameterized behavior to build the collector returned by a method. For example, the method averagingDouble() returns a collector that computes the average of the stream elements. The parameter function mapper with the functional interface type ToDoubleFunction<T> converts an element of type T to a double when the collector computes the average for the stream elements.

Table 16.7 Static Methods in the Collectors Class

Method name (ref.)Return typeFunctional interface parametersFunction type of parameters
averagingDouble
(p. 1000)
Collector<T,?,Double>(ToDoubleFunction<T> mapper)T -> double
averagingInt
(p. 1000)
Collector<T,?,Double>(ToIntFunction<T> mapper)T -> int
averagingLong
(p. 1000)
Collector<T,?,Double>(ToLongFunction<T> mapper)T -> long
collectingAndThen
(p. 997)
Collector<T,A,RR>(Collector<T,A,R> downstream, Function<R,RR> finisher)(T,A) -> R, R -> RR
counting
(p. 998)
Collector<T,?,Long>() 
filtering
(p. 992)
Collector<T,?,R>(Predicate<T> predicate, Collector<T,A,R> downstream)T -> boolean,

(T,A) -> R
flatMapping
(p. 994)
Collector<T,?,R>(Function<T, Stream<U>> mapper, Collector<U,A,R> downstream)T->Stream<U>,

(U,A) -> R
groupingBy
(p. 985)
Collector<T,?, Map<K,List<T>>>(Function<T,K> classifier)T -> K
groupingBy
(p. 985)
Collector<T,?, Map<K,D>>(Function<T,K> classifier, Collector<T,A,D> downstream)T -> K,
(T,A) -> D
groupingBy
(p. 985)
Collector<T,?,Map<K,D>>(Function<T,K> classifier, Supplier<Map<K,D>> mapSupplier, Collector<T,A,D> downstream)T -> K,

()->Map<K,D>,

(T,A)->D
joining
(p. 984)
Collector
<CharSequence,?,String>
() 
joining
(p. 984)
Collector
<CharSequence,?,String>
(CharSequence delimiter) 
joining
(p. 984)
Collector
<CharSequence,?,String>
(CharSequence delimiter, CharSequence prefix, CharSequence suffix) 
mapping
(p. 993)
Collector<T,?,R>(Function<T,U> mapper, Collector<U,A,R> downstream)T -> U,
(U,A) -> R
maxBy
(p. 999)
Collector<T,?,Optional<T>>(Comparator<T> comparator)(T,T) -> T
minBy
(p. 999)
Collector<T,?,Optional<T>>(Comparator<T> comparator)(T,T) -> T
partitioningBy
(p. 989)
Collector<T,?,
Map<Boolean,List<T>>>
(Predicate<T> predicate)T -> boolean
partitioningBy
(p. 989)
Collector<T,?,
Map<Boolean,D>>
(Predicate<T> predicate, Collector<T,A,D> downstream)T -> boolean,
(T,A) -> D
reducing
(p. 1002)
Collector<T,?,Optional<T>>(BinaryOperator<T> op)(T,T) -> T
reducing
(p. 1002)
Collector<T,?,T>(T identity, BinaryOperator<T> op)T -> T,
(T,T) -> T
reducing
(p. 1002)
Collector<T,?,U>(U identity, Function<T,U> mapper, BinaryOperator<U> op)U -> U,
T -> U,

(U,U) -> U
summarizingDouble
(p. 1001)
Collector<T,?,
DoubleSummaryStatistics>
(ToDoubleFunction<T> mapper)T -> double
summarizingInt
(p. 1001)
Collector<T,?,
IntSummaryStatistics>
(ToIntFunction<T> mapper)T -> int
summarizingLong
(p. 1001)
Collector<T,?,
LongSummaryStatistics>
(ToLongFunction<T> mapper)T -> long
summingDouble
(p. 978)
Collector<T,?,Double>(ToDoubleFunction<T> mapper)T -> double
summingInt
(p. 978)
Collector<T,?,Integer>(ToIntFunction<T> mapper)T -> int
summingLong
(p. 978)
Collector<T,?,Long>(ToLongFunction<T> mapper)T -> long
toCollection
(p. 979)
Collector<T,?,C>(Supplier<C> collFactory)() -> C
toList
toUnmodifiableList
(p. 980)
Collector<T,?,List<T>>() 
toMap
(p. 981)
Collector<T,?,Map<K,U>>(Function<T,K> keyMapper, Function<T,U> valueMapper)T -> K,
T -> U
toMap
(p. 981)
Collector<T,?,Map<K,U>>(Function<T,K> keyMapper, Function<T,U> valueMapper,
BinaryOperator<U> mergeFunction)
T -> K,
T -> U,
(U,U) -> U
toMap
(p. 981)
Collector<T,?,Map<K,U>>(Function<T,K> keyMapper, Function<T,U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<Map<K,U>> mapSupplier)T -> K,
T -> U,
(U,U) -> U,
()-> Map<K,U>
toSet
toUnmodifiableSet
(p. 980)
Collector<T,?,Set<T>>() 

Table 16.8 shows a comparison of methods in the stream interfaces that perform reduction operations and static factory methods in the Collectors class that implement collectors with equivalent functionality.

Table 16.8 Method Comparison: The Stream Interfaces and the Collectors Class

Method names in the stream interfacesStatic factory method names in the Collectors class
collect (p. 964)collectingAndThen (p. 997)
count (p. 953)counting (p. 998)
filter (p. 912)filtering (p. 992)
flatMap (p. 924)flatMapping (p. 994)
map (p. 921)mapping (p. 993)
max (p. 954)maxBy (p. 999)
min (p. 954)minBy (p. 999)
reduce (p. 955)reducing (p. 1002)
toList (p. 972)toList (p. 980)
average (p. 972)averagingInt, averagingLong, averagingDouble (p. 1001)
sum (p. 972)summingInt, summingLong, summingDouble (p. 978)
summaryStatistics (p. 972)summarizingInt, summarizingLong, summarizingDouble (p. 1001)

Leave a Reply

Your email address will not be published. Required fields are marked *