Java SE API
Collectors.mapping()
Transforms elements before passing them to a downstream collector. Java SE API; example uses Java 21-compatible syntax.
Formal Syntax & Structure
java syntaxjava
static <T,U,A,R> Collector<T,?,R> mapping(Function<? super T,? extends U> mapper, Collector<? super U,A,R> downstream)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| mapper | Function<? super T,? extends U> | Transformation |
| downstream | Collector<? super U,A,R> | Collects mapped elements |
Return Value
Collector<T,?,R>
Transforms elements before passing them to a downstream collector.
Working Example
Example Snippetjava
import java.util.*;import java.util.stream.*;public class Main { public static void main(String[] args) { record Person(String team, String name) {} var people = List.of(new Person("red", "Ada"), new Person("red", "Bo"), new Person("blue", "Cy")); var labels = people.stream().collect(Collectors.groupingBy(Person::team, TreeMap::new, Collectors.mapping(Person::name, Collectors.joining(" / ")))); System.out.println(labels); var mutable = people.stream().map(Person::name).collect(Collectors.toCollection(ArrayList::new)); mutable.add("Dee"); System.out.println(mutable); }}