mapping

Given a function from T to U, adapt a Collector<U,R> to a
Collector<T,R> which operates by applying the provided function to each input
element before the accumulation step.

Type Parameters:

T - Type of values to be accepted

U - Type of values accepted by provided collector

R - Result type of collector

Parameters:

mapper - A function mapping <T> to <U>

downstream - collector which will accept mapped values

Returns:

A collector which applies a function to input values and then provides them to the downstream collector

API Note

The mapping() collectors are most useful when used in a multi-level collection
following a groupingBy or partitioningBy collection.
For example, given a stream of Person, to accumulate the set of last names in
each city:

toMap

Accumulate elements into a Map whose keys are the input elements and whose values are the
result of a mapping function applied to the element. If the input elements contains duplicates
(according to Object.equals(Object)), an IllegalStateException is thrown when the
collection operation is performed.

Type Parameters:

T - The type of the input elements, and the input type of the mapping function

U - The output type of the mapping function

Parameters:

mapper - The mapping function

Returns:

A Collector which collects elements into a Map whose keys are the input elements,
and whose values are the result of applying the mapping function to the input element

toMap

Accumulate elements into a Map whose keys are the input elements, and whose values are derived
from the result of applying a mapping function to the input element. If the input elements contains duplicates
(according to Object.equals(Object)), the mapping function is applied to each equal element, and the
results are merged with the provided merging function.

Type Parameters:

T - The type of the input elements, and the input type of the mapping function

U - The output type of the mapping function

M - The type of the resulting Map

Parameters:

mapper - The mapping function

mapSupplier - A function which provides a new, empty Map into which the results will be inserted

A Collector which collects elements into a Map whose keys are the input elements,
and whose values are the result of applying the mapping function to all input elements equal to the key and
combining them using the merge function

toConcurrentMap

Accumulate elements into a ConcurrentMap whose keys are the input elements and whose values are the
result of a mapping function applied to the element. If the input elements contains duplicates
(according to Object.equals(Object)), an IllegalStateException is thrown when the
collection operation is performed.

This is a concurrent Collector. (TODO need reference).

Type Parameters:

T - The type of the input elements, and the input type of the mapping function

U - The output type of the mapping function

Parameters:

mapper - The mapping function

Returns:

A Collector which collects elements into a Map whose keys are the input elements,
and whose values are the result of applying the mapping function to the input element

toConcurrentMap

Accumulate elements into a ConcurrentMap whose keys are the input elements, and whose values are derived
from the result of applying a mapping function to the input element. If the input elements contains duplicates
(according to Object.equals(Object)), the mapping function is applied to each equal element, and the
results are merged with the provided merging function.

This is a concurrent Collector. (TODO need reference).

Type Parameters:

T - The type of the input elements, and the input type of the mapping function

U - The output type of the mapping function

M - The type of the resulting Map

Parameters:

mapper - The mapping function

mapSupplier - A function which provides a new, empty Map into which the results will be inserted

A Collector which collects elements into a Map whose keys are the input elements,
and whose values are the result of applying the mapping function to all input elements equal to the key and
combining them using the merge function