There are ways to build a custom collector, either by using one of:
- the use of method
Stream.collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
. - the use of method
Collector.of(Supplier<R> supplier, BiConsumer<R,T> accumulator, BinaryOperator<R> combiner, Collector.Characteristics... characteristics)
. - the use of all factories of
Collectors
. - subtyping
Collector
interface.
Could you provide me an example that really necessitate the subtyping of Collector
?
The only thing that I can imagine is the need to store some state in between the supplier
, accumulator
and combiner
. If we need to store a state in between invocations of supplier
only then a subtype of Supplier
fills the requirement, and same reasoning for accumulator
and combiner
alone. My imagination fails to imagine something useful that needs a "global" data sharing during a collect.
If I have a custom data structure, then it is easy to fill the requirements by some method references like Custom::new
, Custom::add
, Custom::addAll
. So custom collector for a custom data structure is not a good example.
Am I missing something obvious?