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.hashCode changes, 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(6);
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:
factory
other
.
factory
entries
.
factory
iterable
.
factory
keys
to values
.
factory
factory
other
. Example:
factory
no setterinherited
no setterinherited
no setterinherited
no setterinherited
no setterinherited
no setterinherited
no setterinherited
no setterinherited
other
to this map.
inherited
newEntries
to this map.
inherited
RK
keys and RV
instances, if necessary.
inherited
inherited
key
.
inherited
value
.
inherited
action
to each key/value pair of the map.
inherited
convert
function.
inherited
inherited
key
, or add a new entry if it isn't there.
inherited
key
and its associated value, if present, from the map.
inherited
test
.
inherited
inherited
key
.
inherited
inherited
inherited
key
, or null
if key
is not in the map.
inherited
key
with the given value
.
inherited
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4