RFR 8071600: Add a flat-mapping collector

Paul Sandoz paul.sandoz at oracle.com
Wed Feb 25 13:20:19 UTC 2015


On Feb 12, 2015, at 5:07 PM, Paul Sandoz <Paul.Sandoz at oracle.com> 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).
> 

Unfortunately there is not currently enough information propagated down from the top-level collect to make an informed decision about whether one could go parallel and collect concurrently within flatMapping.

Paul.



More information about the core-libs-dev mailing list