Seing a Collector as a Gatherer

Viktor Klang viktor.klang at oracle.com
Wed Jan 17 17:02:05 UTC 2024


Hi Rémi,

Yes, this was something I was hoping to get into the preview, but I wasn't sure where that conversion should end up.

There are a few different places where it might go:

Gatherer.of(Collector)
Gatherers.collect(Collector)
Collector.asGatherer()
Collectors.gather(Collector)

I wasn't really convinced where it should go, and I was hesitant to making any changes to existing public interfaces as a "nice to have", so I opted to leave it out for now.

I think people would prefer to see it on Collector as a default method, but as I said before, making changes to Collector wasn't something I was ready to prioritize for the (first) JEP.

Cheers,
√


Viktor Klang
Software Architect, Java Platform Group
Oracle
________________________________
From: core-libs-dev <core-libs-dev-retn at openjdk.org> on behalf of Remi Forax <forax at univ-mlv.fr>
Sent: Wednesday, 17 January 2024 17:08
To: core-libs-dev <core-libs-dev at openjdk.java.net>
Subject: Seing a Collector as a Gatherer

Hello,
I may have overlook that, but it seems there is no method to see a Collector as a Gatherer.

A Gatherer is more general than a Collector and a Gatherer with a greedy integrator that does not call Downstream.push in the intergator and only once is the finisher is basicaly a Collector.

In code:
<E, A, T> Gatherer<E, A, T> asGatherer(Collector<? super E, A, ? extends T> collector) {
  var supplier = collector.supplier();
  var accumulator = collector.accumulator();
  var combiner = collector.combiner();
  var finisher = collector.finisher();
  return Gatherer.of(supplier,
      Gatherer.Integrator.ofGreedy((state, element, _) -> {
        accumulator.accept(state, element);
        return true;
      }),
      combiner,
      (state, downstream) -> downstream.push(finisher.apply(state)));
}

This is eaxctly how Gatherer.fold() works.

Is there a reason why such method does not exist ?

regards,
Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20240117/747f1403/attachment-0001.htm>


More information about the core-libs-dev mailing list