Adapts a Collector accepting elements of type U to one
accepting elements of type T by applying a mapping function to
each input element before accumulation.
<T> | the type of the input elements | |
<U> | type of elements accepted by downstream collector | |
<A> | intermediate accumulation type of the downstream collector | |
<R> | result type of collector | |
mapper | a function to be applied to the input elements | |
downstream | a collector which will accept mapped values |
@apiNote
The mapping() collectors are most useful when used in a
multi-level reduction, such as downstream of a groupingBy or
partitioningBy. For example, given a stream of
Person, to accumulate the set of last names in each city:
Map<City, Set<String>> lastNamesByCity
= people.stream().collect(groupingBy(Person::getCity,
mapping(Person::getLastName, toSet())));
Diagram: Collectors