toSplayTreeMap<TKey, TValue> method

Map<TKey, TValue> toSplayTreeMap<TKey, TValue>(
  1. MapEntry<TKey, TValue> entrySelector(
    1. T element
    ), {
  2. int keyComparer(
    1. TKey k1,
    2. TKey k2
    )?,
  3. bool modifiable = true,
})

Converts the iterable to a SplayTreeMap.

Iterates over the entire iterable, generating a MapEntry from each element with the entrySelector function then saving each generated entry in a HashMap under the generated key.

If a duplicate key is produced, the value generated by a prior element is overwritten. As such, the length of the resulting SplayTreeMap is not guaranteed to be the same length as the iterable.

Example:

void main() {
  var list = [97, 98, 99];
  var result = list.toSplayTreeMap((x) => MapEntry(x, String.fromCodeUnit(x)));

  // Result: { 97: 'a', 98: 'b', 99: 'c' }
}

Implementation

Map<TKey, TValue> toSplayTreeMap<TKey, TValue>(
  MapEntry<TKey, TValue> Function(T element) entrySelector, {
  int Function(TKey k1, TKey k2)? keyComparer,
  bool modifiable = true,
}) {
  keyComparer ??= EqualityComparer.forType<TKey>().sort;

  final map = SplayTreeMap<TKey, TValue>(keyComparer);
  map.addEntries([for (var o in this) entrySelector(o)]);
  if (modifiable) return map;
  return UnmodifiableMapView(map);
}