RFR 8071600: Add a flat-mapping collector
Paul Sandoz
paul.sandoz at oracle.com
Thu Feb 12 16:07:55 UTC 2015
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?
Thanks,
Paul.
More information about the core-libs-dev
mailing list