Multiset<E>.fromIterable constructor

Multiset<E>.fromIterable(
  1. Iterable<Object?> iterable, {
  2. E key(
    1. Object? element
    )?,
  3. int count(
    1. Object? element
    )?,
})

Creates a Multiset where the elements and their occurrence count is computed from an iterable.

The key function specifies the actual elements added to the collection. The default implementation is the identity function. Repetitions are possible and merge into the previously added elements.

The count function specifies the number of elements added to the collection. The default function returns the constant 1.

Implementation

factory Multiset.fromIterable/*<E>*/(
  Iterable<Object?> /*<E>*/ iterable, {
  E Function(Object? /*E*/ element)? key,
  int Function(Object? /*E*/ element)? count,
}) {
  final result = Multiset<E>();
  key ??= (element) => element as E;
  count ??= (element) => 1;
  for (final element in iterable) {
    result.add(key(element), count(element));
  }
  return result;
}