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:
other. entries. iterable. keys to values. other. Example: other to this map. newEntries to this map. RK keys and RV instances, if necessary. key. value. action to each key/value pair of the map. convert function. key, or add a new entry if it isn't there. key and its associated value, if present, from the map. test. key. key, or null if key is not in the map. key with the given value.
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-collection/HashMap-class.html