groupBy<TKey> method

Map<TKey, List<TValue>> groupBy<TKey>(
  1. TKey selector(
    1. TValue
    )
)

Groups elements of this iterable by comparing the selector result.

Implementation

Map<TKey, List<TValue>> groupBy<TKey>(TKey Function(TValue) selector) {
  final map = <TKey, List<TValue>>{};
  forEach((element) {
    final key = selector(element);
    if (map[key] == null) {
      map[key] = [element];
    } else {
      map[key]!.add(element);
    }
  });
  return map;
}