groupBy<K> method

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

Groups elements of this iterable into a Map using the given keySelector.

Similar to Java's Collectors.groupingBy.

Each entry in the resulting map has a key produced by keySelector, and the corresponding value is a list of elements that share that key.

Example:

class Interest {
  final String name;
  final String category;
  Interest(this.name, this.category);
}

final interests = [
  Interest('Football', 'Sports'),
  Interest('Chess', 'Board Games'),
  Interest('Basketball', 'Sports'),
];

final grouped = interests.groupBy((i) => i.category);
print(grouped['Sports']!.map((i) => i.name)); // [Football, Basketball]

Implementation

Map<K, List<T>> groupBy<K>(K Function(T item) keySelector) {
  return group(keySelector);
}