group<K> method
Groups the elements of the iterable by a key returned from the keySelector
function.
Returns a Map where each key is a value returned by keySelector
,
and the corresponding value is a list of all elements in the iterable
that produced that key.
Example:
final items = ['apple', 'banana', 'avocado'];
final grouped = items.group((item) => item[0]);
// grouped = {
// 'a': ['apple', 'avocado'],
// 'b': ['banana']
// }
Implementation
Map<K, List<T>> group<K>(K Function(T item) keySelector) {
final Map<K, List<T>> result = {};
for (final element in this) {
final key = keySelector(element);
result.putIfAbsent(key, () => []).add(element);
}
return result;
}