DefaultMap<K, V> class
The DefaultMap is a Map-like class that calls a factory function to supply missing values while storing values in an internal map.
It overrides the []
operator: when a value for a given key is present
in the internal map, its value is returned. When the value is not set,
the factory function is called to supply the default value.
Using DefaultMap can be useful, for example, for creating a letter counter (for example the ransom note coding exercise). See how it simplifies code, making your more concise, more explicit, and easier to read.
final string = 'mississippi'.split('');
// Without DefaultMap
final counterMap = <String, int>{};
for (final char in string) {
if (!counterMap.containsKey(char)) counterMap[char] = 0;
counterMap[char] = counterMap[char]! + 1;
}
// With DefaultMap
final counterMap = DefaultMap<String, int>(0);
for (final char in string) counterMap[char] += 1;
The class is inspired by Python's
defaultdict
.
Constructors
- DefaultMap(V defaultValue)
-
Creates a DefaultMap with
defaultValue
as fallback. -
DefaultMap.filled(V defaultValue, [Map<
K, V> ? inner]) -
Creates a DefaultMap with
defaultValue
as fallback, andinner
as the map that will serve as the container map for the values. -
DefaultMap.generate(V generator(K key), [Map<
K, V> ? inner]) -
Creates a DefaultMap where the fallback value is generated by the
generator
function.
Properties
-
entries
→ Iterable<
MapEntry< K, V> > -
The map entries of this.
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- isEmpty → bool
-
Whether there is no key/value pair in the map.
no setterinherited
- isNotEmpty → bool
-
Whether there is at least one key/value pair in the map.
no setterinherited
-
keys
→ Iterable<
K> -
The keys of this.
no setteroverride
- length → int
-
The number of key/value pairs in the map.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
values
→ Iterable<
V> -
The values of this.
no setterinherited
Methods
-
addAll(
Map< K, V> other) → void -
Adds all key/value pairs of
other
to this map.inherited -
addEntries(
Iterable< MapEntry< newEntries) → voidK, V> > -
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 andRV
instances, if necessary.inherited -
clear(
) → void -
Removes all entries from the map.
override
-
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> transform(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 -
remove(
covariant K key) → V -
Removes value for
key
.override -
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 [](
covariant K key) → V -
Gets value for
key
.override -
operator []=(
K key, V value) → void -
Associates the
key
with the givenvalue
.override