SplayTreeMap<K,V> classfinal
AMap of objects that can be ordered relative to each other.
The map is based on a self-balancing binary tree.It allows most single-entry operations in amortized logarithmic time.
Keys of the map are compared using thecompare function passed inthe constructor, both for ordering and for equality.If the map contains only the keya, thenmap.containsKey(b)will returntrue if and only ifcompare(a, b) == 0,and the value ofa == b is not even checked.If the compare function is omitted, the objects are assumed to beComparable, and are compared using theirComparable.compareTo method.Non-comparable objects (includingnull) will not work as keysin that case.
To allow callingoperator [],remove orcontainsKey with objectsthat are not supported by thecompare function, an extraisValidKeypredicate function can be supplied. This function is tested beforeusing thecompare function on an argument value that may not be aKvalue. If omitted, theisValidKey function defaults to testing if thevalue is aK.
Notice:Do not modify a map (add or remove keys) while an operationis being performed on that map, for example in functionscalled during aforEach orputIfAbsent call,or while iterating the map (keys,values orentries).
Example:
final planetsByMass = SplayTreeMap<double, String>((a, b) => a.compareTo(b));To add data to a map, useoperator[]=,addAll oraddEntries.
planetsByMass[0.06] = 'Mercury';planetsByMass .addAll({0.81: 'Venus', 1.0: 'Earth', 0.11: 'Mars', 317.83: 'Jupiter'});To check if the map is empty, useisEmpty orisNotEmpty.To find the number of map entries, uselength.
print(planetsByMass.isEmpty); // falseprint(planetsByMass.length); // 5TheforEach method calls a function for each key/value entry of the map.
planetsByMass.forEach((key, value) { print('$key \t $value'); // 0.06 Mercury // 0.11 Mars // 0.81 Venus // 1.0 Earth // 317.83 Jupiter});To check whether the map has an entry with a specific key, usecontainsKey.
final keyOneExists = planetsByMass.containsKey(1.0); // truefinal keyFiveExists = planetsByMass.containsKey(5); // falseTo check whether the map has an entry with a specific value,usecontainsValue.
final earthExists = planetsByMass.containsValue('Earth'); // truefinal plutoExists = planetsByMass.containsValue('Pluto'); // falseTo remove an entry with a specific key, useremove.
final removedValue = planetsByMass.remove(1.0);print(removedValue); // EarthTo remove multiple entries at the same time, based on their keys and values,useremoveWhere.
planetsByMass.removeWhere((key, value) => key <= 1);print(planetsByMass); // {317.83: Jupiter}To conditionally add or modify a value for a specific key, depending onwhether there already is an entry with that key,useputIfAbsent orupdate.
planetsByMass.update(1, (v) => '', ifAbsent: () => 'Earth');planetsByMass.putIfAbsent(317.83, () => 'Another Jupiter');print(planetsByMass); // {1.0: Earth, 317.83: Jupiter}To update the values of all keys, based on the existing key and value,useupdateAll.
planetsByMass.updateAll((key, value) => 'X');print(planetsByMass); // {1.0: X, 317.83: X}To remove all entries and empty the map, useclear.
planetsByMass.clear();print(planetsByMass.isEmpty); // falseprint(planetsByMass); // {}See also:
- Map, the general interface of key/value pair collections.
- HashMap is unordered (the order of iteration is not guaranteed).
- LinkedHashMap iterates in key insertion order.
- Mixed-in types
- MapMixin<
K,V>
- MapMixin<
Constructors
- SplayTreeMap([intcompare(Kkey1,Kkey2)?,boolisValidKey(dynamicpotentialKey)?])
- SplayTreeMap.from(Map<
Object?,Object?> other, [intcompare(Kkey1,Kkey2)?,boolisValidKey(dynamicpotentialKey)?]) - Creates aSplayTreeMap that contains all key/value pairs of
other.factory - SplayTreeMap.fromIterable(Iterableiterable, {Kkey(dynamicelement)?,Vvalue(dynamicelement)?,intcompare(Kkey1,Kkey2)?,boolisValidKey(dynamicpotentialKey)?})
- Creates aSplayTreeMap where the keys and values are computed from the
iterable.factory - SplayTreeMap.fromIterables(Iterable<
K> keys,Iterable<V> values, [intcompare(Kkey1,Kkey2)?,boolisValidKey(dynamicpotentialKey)?]) - Creates aSplayTreeMap associating the given
keystovalues.factory - SplayTreeMap.of(Map<
K,V> other, [intcompare(Kkey1,Kkey2)?,boolisValidKey(dynamicpotentialKey)?]) - Creates aSplayTreeMap that contains all key/value pairs of
other.Example:factory
Properties
- entries→Iterable<
MapEntry< K,V> > - The map entries of thisMap.no setteroverride
- hashCode→int
- The hash code for this object.no setterinherited
- isEmpty→bool
- Whether there is no key/value pair in the map.no setteroverride
- isNotEmpty→bool
- Whether there is at least one key/value pair in the map.no setteroverride
- keys→Iterable<
K> - The keys of thisMap.no setteroverride
- length→int
- The number of key/value pairs in the map.no setteroverride
- runtimeType→Type
- A representation of the runtime type of the object.no setterinherited
- values→Iterable<
V> - The values of thisMap.no setteroverride
Methods
- addAll(
Map< K,V> other)→ void - Adds all key/value pairs of
otherto this map.override - addEntries(
Iterable< MapEntry< newEntries)→ voidK,V> > - Adds all key/value pairs of
newEntriesto this map.inherited - cast<
RK,RV> ()→Map< RK,RV> - Provides a view of this map as having
RKkeys andRVinstances,if necessary.inherited - clear(
)→ void - Removes all entries from the map.override
- containsKey(
Object?key)→bool - Whether this map contains the given
key.override - containsValue(
Object?value)→bool - Whether this map contains the given
value.override - firstKey(
)→ K? - The first key in the map.
- firstKeyAfter(
Kkey)→ K? - Get the first key in the map that is strictly larger than
key. Returnsnullif such a key was not found. - forEach(
voidf(Kkey,Vvalue))→ void - Applies
actionto each key/value pair of the map.override - lastKey(
)→ K? - The last key in the map.
- lastKeyBefore(
Kkey)→ K? - The last key in the map that is strictly smaller than
key. - map<
K2,V2> (MapEntry< K2,V2> transform(Kkey,Vvalue))→Map<K2,V2> - Returns a new map where all entries of this map are transformed bythe given
convertfunction.inherited - noSuchMethod(
Invocationinvocation)→ dynamic - Invoked when a nonexistent method or property is accessed.inherited
- putIfAbsent(
Kkey,VifAbsent())→ V - Look up the value of
key, or add a new entry if it isn't there.override - remove(
Object?key)→ V? - Removes
keyand its associated value, if present, from the map.override - removeWhere(
booltest(Kkey,Vvalue))→ void - Removes all entries of this map that satisfy the given
test.inherited - toString(
)→String - A string representation of this object.inherited
- update(
Kkey,Vupdate(Vvalue), {VifAbsent()?})→ V - Updates the value for the provided
key.override - updateAll(
Vupdate(Kkey,Vvalue))→ void - Updates all values.override
Operators
- operator ==(
Objectother)→bool - The equality operator.inherited
- operator [](
Object?key)→ V? - The value for the given
key, ornullifkeyis not in the map.override - operator []=(
Kkey,Vvalue)→ void - Associates the
keywith the givenvalue.override