lists<K> method

  1. @useResult
Map<K, List<E>> lists<K>({
  1. required Select<E, K> by,
})

Groups the iterable's elements in lists by keys returned by by.

The order of elements grouped in a list is non-deterministic when the iterable is unordered, i.e. HashSet.

Example

final list = [('a', 1), ('a', 2), ('b', 4)];
final aggregate = list.group.lists(by: (e) => e.$1);

print(aggregate); // {'a': [('a', 1), ('a', 2)], 'b': [('b', 4)]}

Implementation details

Computing K is assumed to be cheap. Hence, Ks are recomputed each time rather than cached.

Implementation

@useResult Map<K, List<E>> lists<K>({required Select<E, K> by}) {
  final results = <K, List<E>>{};
  for (final element in _iterable) {
    (results[by(element)] ??= []).add(element);
  }

  return results;
}