Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:collection
  3. SplayTreeMap<K,V> class
SplayTreeMap
description

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); // 5

TheforEach 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); // false

To check whether the map has an entry with a specific value,usecontainsValue.

final earthExists = planetsByMass.containsValue('Earth'); // truefinal plutoExists = planetsByMass.containsValue('Pluto'); // false

To remove an entry with a specific key, useremove.

final removedValue = planetsByMass.remove(1.0);print(removedValue); // Earth

To 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

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 ofother.
factory
SplayTreeMap.fromIterable(Iterableiterable, {Kkey(dynamicelement)?,Vvalue(dynamicelement)?,intcompare(Kkey1,Kkey2)?,boolisValidKey(dynamicpotentialKey)?})
Creates aSplayTreeMap where the keys and values are computed from theiterable.
factory
SplayTreeMap.fromIterables(Iterable<K>keys,Iterable<V>values, [intcompare(Kkey1,Kkey2)?,boolisValidKey(dynamicpotentialKey)?])
Creates aSplayTreeMap associating the givenkeys tovalues.
factory
SplayTreeMap.of(Map<K,V>other, [intcompare(Kkey1,Kkey2)?,boolisValidKey(dynamicpotentialKey)?])
Creates aSplayTreeMap that contains all key/value pairs ofother.Example:
factory

Properties

entriesIterable<MapEntry<K,V>>
The map entries of thisMap.
no setteroverride
hashCodeint
The hash code for this object.
no setterinherited
isEmptybool
Whether there is no key/value pair in the map.
no setteroverride
isNotEmptybool
Whether there is at least one key/value pair in the map.
no setteroverride
keysIterable<K>
The keys of thisMap.
no setteroverride
lengthint
The number of key/value pairs in the map.
no setteroverride
runtimeTypeType
A representation of the runtime type of the object.
no setterinherited
valuesIterable<V>
The values of thisMap.
no setteroverride

Methods

addAll(Map<K,V>other)→ void
Adds all key/value pairs ofother to this map.
override
addEntries(Iterable<MapEntry<K,V>>newEntries)→ void
Adds all key/value pairs ofnewEntries to this map.
inherited
cast<RK,RV>()Map<RK,RV>
Provides a view of this map as havingRK keys andRV instances,if necessary.
inherited
clear()→ void
Removes all entries from the map.
override
containsKey(Object?key)bool
Whether this map contains the givenkey.
override
containsValue(Object?value)bool
Whether this map contains the givenvalue.
override
firstKey()→ K?
The first key in the map.
firstKeyAfter(Kkey)→ K?
Get the first key in the map that is strictly larger thankey. Returnsnull if such a key was not found.
forEach(voidf(Kkey,Vvalue))→ void
Appliesaction to 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 thankey.
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 givenconvert function.
inherited
noSuchMethod(Invocationinvocation)→ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
putIfAbsent(Kkey,VifAbsent())→ V
Look up the value ofkey, or add a new entry if it isn't there.
override
remove(Object?key)→ V?
Removeskey and its associated value, if present, from the map.
override
removeWhere(booltest(Kkey,Vvalue))→ void
Removes all entries of this map that satisfy the giventest.
inherited
toString()String
A string representation of this object.
inherited
update(Kkey,Vupdate(Vvalue), {VifAbsent()?})→ V
Updates the value for the providedkey.
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 givenkey, ornull ifkey is not in the map.
override
operator []=(Kkey,Vvalue)→ void
Associates thekey with the givenvalue.
override
  1. Dart
  2. dart:collection
  3. SplayTreeMap<K,V> class
dart:collection library

[8]ページ先頭

©2009-2025 Movatter.jp