HashMap<K, V>.fromEntries constructor

  1. @Since("2.1")
HashMap<K, V>.fromEntries(
  1. Iterable<MapEntry<K, V>> entries
)

Creates a HashMap containing the entries of entries.

Returns a new HashMap<K, V> where all entries of entries have been added in iteration order.

If multiple entries have the same key, later occurrences overwrite the earlier ones.

Example:

final numbers = [11, 12, 13, 14];
final map = HashMap.fromEntries(numbers.map((i) => MapEntry(i, i * i)));
print(map); // {11: 121, 12: 144, 13: 169, 14: 196}

Implementation

@Since("2.1")
factory HashMap.fromEntries(Iterable<MapEntry<K, V>> entries) =>
    HashMap<K, V>()..addEntries(entries);