classify<K> method

Map<K, Set<E>> classify<K>(
  1. K classifier(
    1. E element
    )
)

Returns a map grouping all elements of this with the same value for classifier.

Example:

{'aaa', 'bbb', 'cc', 'a', 'bb'}.classify<int>((e) => e.length);
// Returns {
//   1: {'a'},
//   2: {'cc', 'bb'},
//   3: {'aaa', 'bbb'}
// }

Implementation

Map<K, Set<E>> classify<K>(K classifier(E element)) {
  final groups = <K, Set<E>>{};
  for (var e in this) {
    groups.putIfAbsent(classifier(e), () => <E>{}).add(e);
  }
  return groups;
}