Java 16: Stream.mapMulti for one-to-many mapping

mapMulti pushes zero-or-more results per element without creating intermediate streams.

Code
List<Integer> nums = List.of(1, 2, 3);
List<Integer> withDoubles = nums.stream()
        .<Integer>mapMulti((n, consumer) -> {
            consumer.accept(n);
            consumer.accept(n * 10);
        })
        .toList();
System.out.println(withDoubles);
Output
[1, 10, 2, 20, 3, 30]
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-26