groupBy<K> method

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

Group the list by f. Returns a Map with the keys and values returned by f.

Implementation

Map<K, List<T>> groupBy<K>(K Function(T) f) {
  final map = <K, List<T>>{};
  for (final item in this) {
    final key = f(item);
    if (map.containsKey(key)) {
      map[key]!.add(item);
    } else {
      map[key] = [item];
    }
  }
  return map;
}