RFR 8071600: Add a flat-mapping collector

Peter Levart peter.levart at gmail.com
Thu Feb 12 16:12:10 UTC 2015


On 02/12/2015 05:07 PM, Paul Sandoz wrote:
> On Feb 12, 2015, at 4:51 PM, Peter Levart <peter.levart at gmail.com> wrote:
>> Hi Paul,
>>
>> Would the following "optimization" make any sense?
>>
>>      public static <T, U, A, R>
>>      Collector<T, ?, R> flatMapping(Function<? super T, ? extends Stream<? extends U>> mapper,
>>                                     Collector<? super U, A, R> downstream) {
>>          BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator();
>>          return new CollectorImpl<>(downstream.supplier(),
>>              (r, t) -> {
>>                  try (Stream<? extends U> result = mapper.apply(t)) {
>>                      if (result != null) {
>>                          ((result.isParallel() &&
>>                              !downstream.characteristics().contains(Collector.Characteristics.CONCURRENT))
>>                              ? result.sequential() : result).forEach(u -> downstreamAccumulator.accept(r, u));
>>                      }
>>                  }
>>              },
>>              downstream.combiner(), downstream.finisher(),
>>              downstream.characteristics());
>>      }
>>
> Ah, interesting.
>
> Possibly... i need to think thought the implications on stream and collectors (groupingByConcurrent does the right thing for a non-concurrent downstream collector and synchronizes on the accumulation).
>
> Do you mind if i treat that as a separate issue?

I don't mind, of course. It was just a thought.

Regards, Peter

> Thanks,
> Paul.




More information about the core-libs-dev mailing list