Returns a
Collector that accumulates elements into a
Map whose keys and values are the result of applying the provided
mapping functions to the input elements.
If the mapped keys contain duplicates (according to
Object#equals(Object)), an
IllegalStateException is
thrown when the collection operation is performed. If the mapped keys
might have duplicates, use
#toMap(Function,Function,BinaryOperator)instead.
There are no guarantees on the type, mutability, serializability,
or thread-safety of the
Map returned.
API Note:
It is common for either the key or the value to be the input elements.
In this case, the utility method
java8.util.function.Functions#identity() may be helpful.
For example, the following produces a
Map mapping
students to their grade point average:
Map studentToGPA =
And the following produces a
Map mapping a unique identifier to
students:
Map studentIdToStudent =
Implementation Note:
The returned
Collector is not concurrent. For parallel stream
pipelines, the
combiner function operates by merging the keys
from one map into another, which can be an expensive operation. If it is
not required that results are inserted into the
Map in encounter
order, using
#toConcurrentMap(Function,Function)may offer better parallel performance.