Building a Custom Collector
Implement the Collector interface to create a reusable aggregation for a domain-specific use case.
Why Build a Custom Collector?
When built-in collectors don't fit your domain, implement the Collector<T, A, R> interface to create reusable, composable aggregation logic. T=input, A=mutable accumulator, R=result.
The Collector Interface
A Collector has four components:
supplier()— creates the initial accumulatoraccumulator()— folds one element into the accumulatorcombiner()— merges two accumulators (for parallel streams)finisher()— transforms the accumulator into the final resultcharacteristics()— optimization hints
All lessons in this course
- groupingBy: Classifying Elements
- partitioningBy and counting
- toMap, joining, and summarizing
- Building a Custom Collector