groupByTransform<K, V> method

KtMap<K, KtList<V>> groupByTransform<K, V>(
  1. K keySelector(
    1. T
    ),
  2. V valueTransform(
    1. T
    )
)

Groups values returned by the valueTransform function applied to each element of the original collection by the key returned by the given keySelector function applied to the element and returns a map where each group key is associated with a list of corresponding values.

The returned map preserves the entry iteration order of the keys produced from the original collection.

Implementation

KtMap<K, KtList<V>> groupByTransform<K, V>(
    K Function(T) keySelector, V Function(T) valueTransform) {
  final groups = linkedMapFrom<K, KtList<V>>();
  for (final element in iter) {
    final key = keySelector(element);
    final list = KtMutableMapExtensions(groups)
        .getOrPut(key, () => mutableListOf<V>()) as KtMutableList<V>;
    list.add(valueTransform(element));
  }
  return groups;
}