groupBy<S, T> function

Map<T, List<S>> groupBy <S, T>(
  1. Iterable<S> values,
  2. T key(
    1. S element
    )
)

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<T, List<S>> groupBy<S, T>(Iterable<S> values, T key(S element)) {
  var map = <T, List<S>>{};
  for (var element in values) {
    var list = map.putIfAbsent(key(element), () => []);
    list.add(element);
  }
  return map;
}