185. Working with mapMulti()
Starting with JDK 16, the Stream API was enriched with a new intermediate operation, named mapMulti()
. This operation is represented by the following default
method in the Stream
interface:
default <R> Stream<R> mapMulti (
BiConsumer<? super T, ? super Consumer<R>> mapper)
Let’s follow the learning-by-example approach and consider the next classical example, which uses a combination of filter()
and map()
to filter even integers and double their value:
List<Integer> integers = List.of(3, 2, 5, 6, 7, 8);
List<Integer> evenDoubledClassic = integers.stream()
.filter(i -> i % 2 == 0)
.map(i -> i * 2)
.collect(toList());
The same result can be obtained via mapMulti()
as follows:
List<Integer> evenDoubledMM = integers.stream()
.<Integer>mapMulti((i, consumer) -> {
if (i % 2 == 0) {
consumer.accept(i * 2);
}
})
.collect(toList());
So instead...