SplayTreeMap.fromIterable constructor - SplayTreeMap - dart:collection library (original) (raw)
SplayTreeMap<K, V>.fromIterable(
- Iterable iterable, {
- K key(
- dynamic element
)?,
- dynamic element
- V value(
- dynamic element
)?,
- dynamic element
- int compare(
- K key1,
- K key2
)?,
- bool isValidKey(
- dynamic potentialKey
)?, })
- dynamic potentialKey
Creates a SplayTreeMap where the keys and values are computed from theiterable
.
For each element of the iterable
this constructor computes a key/value pair, by applying key
and value
respectively.
The keys of the key/value pairs do not need to be unique. The last occurrence of a key will simply overwrite any previous value.
If no functions are specified for key
and value
, the default is to use the iterable value itself. Example:
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}
Implementation
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;
}