HashMap class - dart:collection library (original) (raw)
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).
Do not modify keys in any way which changes their equality (and thus their hash code) while they are in the map. If a map key's Object.hashCodechanges, it may cause future lookups for that key to fail.
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
- Map<K, V>
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 theiterable
.
factory
HashMap.fromIterables(Iterable<K> keys, Iterable<V> values)
Creates a HashMap associating the given keys
to values
.
factory
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 Map.
no setterinherited
The hash code for this object.
no setterinherited
Whether there is no key/value pair in the map.
no setterinherited
Whether there is at least one key/value pair in the map.
no setterinherited
The keys of this Map.
no setterinherited
The number of key/value pairs in the map.
no setterinherited
A representation of the runtime type of the object.
no setterinherited
The values of this Map.
no setterinherited
Methods
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
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 nonexistent 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
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
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