HashMap<K, V> class Null safety

A hash-table based implementation of Map.

The HashMap is unordered (the order of iteration is not guaranteed).

The keys of a HashMap must have consistent Object.== and Object.hashCode implementations. This means that the == operator must define a stable equivalence relation on the keys (reflexive, symmetric, transitive, and consistent over time), and that hashCode must be the same for objects that are considered equal by ==.

Iterating the map's keys, values or entries (through forEach) may happen in any order. The iteration order only changes when the map is modified. Values are iterated in the same order as their associated keys, so iterating the keys and values in parallel will give matching key and value pairs.

Notice: Do not modify a map (add or remove keys) while an operation is being performed on that map, for example in functions called during a forEach or putIfAbsent call, or while iterating the map (keys, values or entries).


final Map<int, String> planets = HashMap(); // Is a HashMap

To add data to a map, use operator[]=, addAll or addEntries.

planets[3] = 'Earth';
planets.addAll({4: 'Mars'});
final gasGiants = {6: 'Jupiter', 5: 'Saturn'};
print(planets); // fx {5: Saturn, 6: Jupiter, 3: Earth, 4: Mars}

To check if the map is empty, use isEmpty or isNotEmpty. To find the number of map entries, use length.

final isEmpty = planets.isEmpty; // false
final length = planets.length; // 4

The forEach iterates through all entries of a map.

planets.forEach((key, value) {
  print('$key \t $value');
  // 5        Saturn
  // 4        Mars
  // 3        Earth
  // 6        Jupiter

To check whether the map has an entry with a specific key, use containsKey.

final keyOneExists = planets.containsKey(4); // true
final keyFiveExists = planets.containsKey(1); // false

To check whether the map has an entry with a specific value, use containsValue.

final marsExists = planets.containsValue('Mars'); // true
final venusExists = planets.containsValue('Venus'); // false

To remove an entry with a specific key, use remove.

final removeValue = planets.remove(5);
print(removeValue); // Jupiter
print(planets); // fx {4: Mars, 3: Earth, 5: Saturn}

To remove multiple entries at the same time, based on their keys and values, use removeWhere.

planets.removeWhere((key, value) => key == 5);
print(planets); // fx {3: Earth, 4: Mars}

To conditionally add or modify a value for a specific key, depending on whether there already is an entry with that key, use putIfAbsent or update.

planets.update(4, (v) => 'Saturn');
planets.update(8, (v) => '', ifAbsent: () => 'Neptune');
planets.putIfAbsent(4, () => 'Another Saturn');
print(planets); // fx {4: Saturn, 8: Neptune, 3: Earth}

To update the values of all keys, based on the existing key and value, use updateAll.

planets.updateAll((key, value) => 'X');
print(planets); // fx {8: X, 3: X, 4: X}

To remove all entries and empty the map, use clear.

print(planets); // {}
print(planets.isEmpty); // true

See also:

  • Map, the general interface of key/value pair collections.
  • LinkedHashMap iterates in key insertion order.
  • SplayTreeMap iterates the keys in sorted order.
Implemented types


HashMap({bool equals(K, K)?, int hashCode(K)?, bool isValidKey(dynamic)?})
Creates an unordered hash-table based Map.
HashMap.from(Map other)
Creates a HashMap that contains all key/value pairs of other.
HashMap.fromEntries(Iterable<MapEntry<K, V>> entries)
Creates a HashMap containing the entries of entries.
HashMap.fromIterable(Iterable iterable, {K key(dynamic element)?, V value(dynamic element)?})
Creates a HashMap where the keys and values are computed from the iterable.
HashMap.fromIterables(Iterable<K> keys, Iterable<V> values)
Creates a HashMap associating the given keys to values.
Creates an unordered identity-based map.
HashMap.of(Map<K, V> other)
Creates a HashMap that contains all key/value pairs of other. Example:


entries Iterable<MapEntry<K, V>>
The map entries of this.
read-only, inherited
hashCode int
The hash code for this object.
read-only, inherited
isEmpty bool
Whether there is no key/value pair in the map.
read-only, inherited
isNotEmpty bool
Whether there is at least one key/value pair in the map.
read-only, inherited
keys Iterable<K>
The keys of this.
read-only, inherited
length int
The number of key/value pairs in the map.
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
values Iterable<V>
The values of this.
read-only, inherited


addAll(Map<K, V> other) → void
Adds all key/value pairs of other to this map.
addEntries(Iterable<MapEntry<K, V>> newEntries) → void
Adds all key/value pairs of newEntries to this map.
cast<RK, RV>() Map<RK, RV>
Provides a view of this map as having RK keys and RV instances, if necessary.
clear() → void
Removes all entries from the map.
containsKey(Object? key) bool
Whether this map contains the given key.
containsValue(Object? value) bool
Whether this map contains the given value.
forEach(void action(K key, V value)) → void
Applies action to each key/value pair of the map.
map<K2, V2>(MapEntry<K2, V2> convert(K key, V value)) Map<K2, V2>
Returns a new map where all entries of this map are transformed by the given convert function.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
putIfAbsent(K key, V ifAbsent()) → V
Look up the value of key, or add a new entry if it isn't there.
remove(Object? key) → V?
Removes key and its associated value, if present, from the map.
removeWhere(bool test(K key, V value)) → void
Removes all entries of this map that satisfy the given test.
toString() String
A string representation of this object.
update(K key, V update(V value), {V ifAbsent()?}) → V
Updates the value for the provided key.
updateAll(V update(K key, V value)) → void
Updates all values.


operator ==(Object other) bool
The equality operator.
operator [](Object? key) → V?
The value for the given key, or null if key is not in the map.
operator []=(K key, V value) → void
Associates the key with the given value.