IMap<K, V>.fromValues constructor

IMap<K, V>.fromValues({
  1. required K keyMapper(
    1. V
    ),
  2. required Iterable<V> values,
  3. ConfigMap? config,
})

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

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

Implementation

factory IMap.fromValues({
  required K Function(V) keyMapper,
  required Iterable<V> values,
  ConfigMap? config,
}) {
  config ??= defaultConfig;

  Map<K, V> map = ListMap.fromEntries(
    values.map((value) => MapEntry(keyMapper(value), value)),
    sort: config.sort,
  );

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