TreeMap¶
Constructor Summary¶
Constructor |
Description |
---|---|
Constructs a new, empty TreeMap. You can supply a
comparator if your keys do not implement rich
comparison methods (you do not need this for most
Python built-in objects).
|
Methods Summary¶
Method |
Description |
---|---|
Returns the number of entries in the tree.
|
|
Returns
True if this map contains a mappingfor the specified key. Returns
False otherwise. |
|
Returns
True if at least 1 key is mapped tothe specified value. Returns
False otherwise. |
|
Returns the value to which the specified key is mapped
to. Returns
None is no such mapping exists. |
|
Returns the comparator if you supplied one.
Returns
None otherwise. |
|
Returns the first/smallest/left-most key.
KeyError is raised if no such key exists |
|
Returns the last/largest/right-most key.
KeyError is raised if no such key exists |
|
Associates the key with the value. If the key
already exists, the old associated value is
replaced with the new value.
|
|
If the key exists, remove the entry and return the
associated value. Returns
None otherwise. |
|
Removes all entries and reset all the fields.
|
|
Returns the first/smallest/left-most entry.
Returns
None if no such entry exists. |
|
Returns the last/largest/right-most entry.
Returns
None if no such entry exists. |
|
Removes and returns the first entry.
Returns
None if no such entry exists. |
|
Removes and returns the last entry.
Returns
None if no such entry exists. |
|
Returns the entry with the greatest key
less than the specified key.
Returns
None if no such entry exists. |
|
Returns the greatest key less than
the specified key.
Returns
None if no such key exists. |
|
Returns the entry with the greatest key
less than or equal to the specified key.
Returns
None if no such entry exists. |
|
Returns the greatest key less than or equal
to the specified key.
Returns
None if no such key exists. |
|
Returns the entry with the smallest key
greater than or equal to the specified key.
Returns
None if no such entry exists. |
|
Returns the smallest key
greater than or equal to the specified key.
Returns
None if no such key exists. |
|
Returns the entry with the smallest key
greater than the specified key.
Returns
None if no such entry exists. |
|
Returns the smallest key greater than
the specified key.
Returns
None if no such key exists. |
|
Returns a set view of all the keys in the map.
The object returned is iterable and the iteration
goes from the smallest to the largest key.
|
|
Same as
key_set() . |
|
Similar to
key_set() but theiteration goes from the largest to the smallest key.
|
|
Returns a collection view of the values in the map.
The object returned is iterable.
|
|
Returns a set view of the entries in the map.
The object returned is iterable and the iteration
from the entry with the smallest to the entry with
the largest key.
|
|
Returns a reverse order view of the map.
|
|
Returns a view of the portion of this map whose
keys range from from_key to to_key.
The default includes from_key but excludes to_key.
|
|
Returns a view of the portion of this map whose
keys are less than (or equal to, if inclusive is
true) to_key.
|
|
Returns a view of the portion of this map whose
keys are greater than (or equal to, if inclusive
is true) from_key.
|
Methods is_empty()
, put_all()
, equals()
, hash_code()
,
to_string()
, get_or_default()
, for_each()
, replace_all()
,
put_if_absent()
, replace()
, compute_if_absent()
,
compute_if_present()
, compute()
and classes SimpleEntry
,
SimpleImmutableEntry
, Entry
are also implemented. For advanced users,
please consult the source code.
Constructor and Methods Details¶
-
class
TreeMap
(comparator=None)¶ Constructs a new, empty TreeMap. You can supply a comparator if your keys do not implement rich comparison methods (you do not need this for most Python built-in objects). The comparator function should take 2 arguments
a
andb
, return a negative int ifa < b
, return a positive int ifa > b
, and return 0 ifa == b
. E.g.def cmp(a, b): if a < b: return -1 elif a > b: return 1 else: return 0
-
size
()¶ Returns the number of entries in the tree.
Tip
You can also use the built-in
len()
function to get the size of the tree.
-
contains_key
(key)¶ Returns
True
if this map contains a mapping for the specified key. ReturnsFalse
otherwise.- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
Tip
You can also use the
key in self
syntax to check whether the map contains the ky.
-
contains_value
(value)¶ Returns``True`` if at least 1 key is mapped to the specified value. Returns
False
otherwise.
-
get
(key)¶ Returns the value to which the specified key is mapped to. Returns
None
is no such mapping exists.- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
Tip
You can also get the value using the
self[key]
syntax. Unlikeget()
, aKeyError
is raised if the key is not found.
-
comparator
()¶ Returns the comparator if you supplied one. Returns
None
otherwise.
-
first_key
()¶ Returns the first/smallest/left-most key.
- Raises
KeyError – if the map is empty
-
last_key
()¶ Returns the last/largest/right-most key.
- Raises
KeyError – if the map is empty
-
put
(key, value)¶ Associates the key with the value. If the key already exists, the old associated value is replaced with the new value.
- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
Tip
You can also insert a key-value pair using the
self[key] = value
syntax.
-
remove
(key)¶ If the key exists, remove the entry and return the associated value. Returns
None
otherwise.- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
Tip
You can also remove an entry using the
del self[key]
syntax. Unlikeput()
, aKeyError
is raised if the key is not found.
-
clear
()¶ Removes all entries and reset all the fields.
-
first_entry
()¶ Returns the first/smallest/left-most entry. Returns
None
if no such entry exists.- Return type
-
last_entry
()¶ Returns the last/largest/right-most entry. Returns
None
if no such entry exists.- Return type
-
poll_first_entry
()¶ Removes and returns the first entry. Returns
None
if no such entry exists.- Return type
-
poll_last_entry
()¶ Removes and returns the last entry. Returns
None
if no such entry exists.- Return type
-
lower_entry
(key)¶ Returns the entry with the greatest key less than the specified key. Returns
None
if no such entry exists.- Return type
- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
-
lower_key
(key)¶ Returns the greatest key less than the specified key. Returns
None
if no such key exists.- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
-
floor_entry
(key)¶ Returns the entry with the greatest key less than or equal to the specified key. Returns
None
if no such entry exists.- Return type
- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
-
floor_key
(key)¶ Returns the greatest key less than or equal to the specified key. Returns
None
if no such key exists.- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
-
ceiling_entry
(key)¶ Returns the entry with the smallest key greater than or equal to the specified key. Returns
None
if no such entry exists.- Return type
- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
-
ceiling_key
(key)¶ Returns the smallest key greater than or equal to the specified key. Returns
None
if no such key exists.- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
-
higher_entry
(key)¶ Returns the entry with the smallest key greater than the specified key. Returns
None
if no such entry exists.- Return type
- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
-
higher_key
(key)¶ Returns the smallest key greater than the specified key. Returns
None
if no such key exists.- Raises
TypeError – If
key
is not comparable to the keys currently in the map.
-
key_set
()¶ Returns a set view of all the keys in the map. The object returned is iterable and the iteration goes from the smallest to the largest key.
- Return type
Same as
key_set()
.- Return type
-
descending_key_set
()¶ Similar to
key_set()
but the iteration goes from the largest to the smallest key.- Return type
-
values
()¶ Returns a collection view of the values in the map. The object returned is iterable.
- Return type
-
entry_set
()¶ Returns a set view of the entries in the map. The object returned is iterable and the iteration from the entry with the smallest to the entry with the largest key.
- Return type
-
descending_map
()¶ Returns a reverse order view of the map.
- Return type
-
sub_map
(from_key, to_key, from_inclusive=True, to_inclusive=False)¶ Returns a view of the portion of this map whose keys range from from_key to to_key. The default includes from_key but excludes to_key.
- Return type
- Raises
TypeError – If
from_key
orto_key
is not comparable to the keys currently in the map.KeyError – If
from_key
is greater thanto_key
; or if this map itself has a restricted range, andfrom_key
orto_key
lies outside the bounds of the range.
-
head_map
(to_key, inclusive=False)¶ Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) to_key.
- Raises
TypeError – If
to_key
is not comparable to the keys currently in the map.KeyError – If this map itself has a restricted range, and
to_key
lies outside the bounds of the range.
-
tail_map
(from_key, inclusive=True)¶ Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) from_key.
- Raises
TypeError – If
from_key
is not comparable to the keys currently in the map.KeyError – If this map itself has a restricted range, and
from_key
lies outside the bounds of the range.
-