-
Enhancement
-
Resolution: Fixed
-
P4
-
None
-
b54
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8084586 | emb-9 | Paul Sandoz | P4 | Resolved | Fixed | team |
When collecting elements of a stream and producing a summary result (such as a
multi-map with `groupingBy`) it can tricky to map an element downstream to zero
or more elements after which those mapped elements are further operated on.
A `flatMapping` collector that maps an element to a Stream of zero or more
mapped elements can be added to `Collectors`:
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 Collector.of(downstream.supplier(),
(r, t) -> mapper.apply(t).sequential().forEach(u -> downstreamAccumulator.accept(r, u)),
downstream.combiner(),
downstream.finisher(),
downstream.characteristics().stream().toArray(Collector.Characteristics[]::new));
}
multi-map with `groupingBy`) it can tricky to map an element downstream to zero
or more elements after which those mapped elements are further operated on.
A `flatMapping` collector that maps an element to a Stream of zero or more
mapped elements can be added to `Collectors`:
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 Collector.of(downstream.supplier(),
(r, t) -> mapper.apply(t).sequential().forEach(u -> downstreamAccumulator.accept(r, u)),
downstream.combiner(),
downstream.finisher(),
downstream.characteristics().stream().toArray(Collector.Characteristics[]::new));
}
- backported by
-
JDK-8084586 Add a flat-mapping collector
-
- Resolved
-