groupBy<T, K> method

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

Groups the elements in values by the value returned by key.

Returns a map from keys computed by key to a list of all values for which key returns that key. The values appear in the list in the same relative order as in values.

Implementation

Map<K, List<T>> groupBy<T, K>(K key(T e)) {
  var map = <K, List<T>>{};

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