最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java 8 - Custom collector (implements Collector), do you have a real useful example? - Stack Overflow

programmeradmin5浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论