Creates a Map instance in which the keys and values are computed from the
iterable
.
The created map is a LinkedHashMap.
A LinkedHashMap
requires the keys to implement compatible
operator==
and hashCode
, and it allows null as a key.
It iterates in key insertion order.
For each element of the iterable
this constructor computes a key-value
pair, by applying key
and value
respectively.
The example below creates a new Map from a List. The keys of map
are
list
values converted to strings, and the values of the map
are the
squares of the list
values:
List<int> list = [1, 2, 3];
Map<String, int> map = new Map.fromIterable(list,
key: (item) => item.toString(),
value: (item) => item * item));
map['1'] + map['2']; // 1 + 4
map['3'] - map['2']; // 9 - 4
If no values are specified for key
and value
the default is the
identity function.
In the following example, the keys and corresponding values of map
are list
values:
map = new Map.fromIterable(list);
map[1] + map[2]; // 1 + 2
map[3] - map[2]; // 3 - 2
The keys computed by the source iterable
do not need to be unique. The
last occurrence of a key will simply overwrite any previous value.
Source
factory Map.fromIterable(Iterable iterable, {K key(element), V value(element)}) = LinkedHashMap<K, V>.fromIterable;