IMap<K, V>.fromKeys constructor

IMap<K, V>.fromKeys({
  1. required Iterable<K> keys,
  2. required V valueMapper(
    1. K
    ),
  3. ConfigMap? config,
})

Create an IMap from the given keys. The values will be the result of applying valueMapper to the keys. If a key repeats, later occurrences overwrite the earlier ones.

// Results in {"Jim": 3, "David": 5}
IMap<String, int> imap = IMap.fromKeys(
    ["Jim", "David"], (String name) => name.length);

Implementation

factory IMap.fromKeys({
  required Iterable<K> keys,
  required V Function(K) valueMapper,
  ConfigMap? config,
}) {
  config ??= defaultConfig;

  Map<K, V> map = ListMap.fromEntries(
    keys.map((key) => MapEntry(key, valueMapper(key))),
    sort: config.sort,
  );

  return IMap._(map, config: config);
}