Java 16 introduces `Stream::mapMulti`, an imperative alternative to `flatMap` that accepts a `BiConsumer<T, Consumer<R>>` instead of a function returning a `Stream`. This avoids creating many small `Stream` instances, which can improve performance. The post covers usage examples including filtering `Optional` values and traversing visitor-based data structures, highlights a type inference limitation requiring explicit type witnesses, and presents JMH benchmarks showing `mapMulti` is often faster than `flatMap` (sometimes by 4–14x), though the speedup is inconsistent and can drop below 1x at large element counts. The author recommends `flatMap` as the default but suggests `mapMulti` when stream creation overhead is a concern or when the source structure doesn't easily convert to a `Stream`.
Table of contents
▚ Enter Stream :: mapMulti▚ The Unfortunate Type Witness▚ Stream < Optional > Performance▚ ReflectionSort: