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).

Example:

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'};
planets.addEntries(gasGiants.entries);
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.

planets.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

Constructors

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

Properties

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

Methods

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

Operators

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