associateByTo<K, V, M extends KtMutableMap<K, V>> method

M associateByTo<K, V, M extends KtMutableMap<K, V>>(
  1. M destination,
  2. K keySelector(
    1. T
    ), [
  3. V valueTransform(
    1. T
    )?
])

Populates and returns the destination mutable map with key-value pairs, where key is provided by the keySelector function and and value is provided by the valueTransform function applied to elements of the given collection.

If any two elements would have the same key returned by keySelector the last one gets added to the map.

Implementation

M associateByTo<K, V, M extends KtMutableMap<K, V>>(
    M destination, K Function(T) keySelector,
    [V Function(T)? valueTransform]) {
  for (final element in iter) {
    final key = keySelector(element);
    final V value =
        valueTransform == null ? element as V : valueTransform(element);
    destination.put(key, value);
  }
  return destination;
}