groupBy<T, K> method

Map<K, List<T>> groupBy<T, K>(
  1. K keySelector(
    1. T e
    )
)

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

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

Implementation

Map<K, List<T>> groupBy<T, K>(K keySelector(T e)) {
  if (this == null) return {};
  var map = <K, List<T>>{};

  for (final element in this!) {
    var list = map.putIfAbsent(keySelector(element as T), () => []);
    list.add(element);
  }
  return map;
}