public static <T, K, U> Collector<T, ?, Map<K, U>> toMap (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)

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 contains duplicates (according to Object.equals(Object)), an IllegalStateException is thrown when the collection operation is performed. If the mapped keys may have duplicates, use toMap(Function, Function, BinaryOperator) instead.

Parameters:
<T>    the type of the input elements
<K>    the output type of the key mapping function
<U>    the output type of the value mapping function
keyMapper    a mapping function to produce keys
valueMapper    a mapping function to produce values

Returns:  a Collector which collects elements into a Map whose keys and values are the result of applying mapping functions to the input elements

See also:
toMap(Function, Function, BinaryOperator), toMap(Function, Function, BinaryOperator, Supplier), toConcurrentMap(Function, Function)

@apiNote It is common for either the key or the value to be the input elements. In this case, the utility method java.util.function.Function.identity() may be helpful. For example, the following produces a Map mapping students to their grade point average:


     Map<Student, Double> studentToGPA
         students.stream().collect(toMap(Functions.identity(),
                                         student -> computeGPA(student)));
 
And the following produces a Map mapping a unique identifier to students:

     Map<String, Student> studentIdToStudent
         students.stream().collect(toMap(Student::getId,
                                         Functions.identity());
 

@implNote 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.