SplayTreeMap<K, V>.fromIterable 构造函数

SplayTreeMap<K, V>.fromIterable(
  1. Iterable iterable,
  2. {K key(
    1. dynamic element
    )?,
  3. V value(
    1. dynamic element
    )?,
  4. int compare(
    1. K key1,
    2. K key2
    )?,
  5. bool isValidKey(
    1. dynamic potentialKey
    )?}
)

创建一个由 iterable 计算键和值的 SplayTreeMap

对于 iterable 的每个元素,此构造函数通过分别应用 keyvalue 来计算键/值对。

键/值对的键不需要唯一。键的最后出现将简单地覆盖任何先前值。

如果没有指定 keyvalue 的函数,则默认使用可迭代的值。例如

final numbers = [12, 11, 14, 13];
final mapFromIterable =
    SplayTreeMap<int, int>.fromIterable(numbers,
        key: (i) => i, value: (i) => i * i);
print(mapFromIterable); // {11: 121, 12: 144, 13: 169, 14: 196}

实现

factory SplayTreeMap.fromIterable(Iterable iterable,
    {K Function(dynamic element)? key,
    V Function(dynamic element)? value,
    int Function(K key1, K key2)? compare,
    bool Function(dynamic potentialKey)? isValidKey}) {
  SplayTreeMap<K, V> map = SplayTreeMap<K, V>(compare, isValidKey);
  MapBase._fillMapWithMappedIterable(map, iterable, key, value);
  return map;
}