TreeMap

Constructor Summary

Constructor

Description

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).

Methods Summary

Method

Description

size()

Returns the number of entries in the tree.

contains_key(key)

Returns True if this map contains a mapping
for the specified key. Returns False otherwise.

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.

comparator()

Returns the comparator if you supplied one.
Returns None otherwise.

first_key()

Returns the first/smallest/left-most key.
KeyError is raised if no such key exists

last_key()

Returns the last/largest/right-most key.
KeyError is raised if no such key exists

put(key, value)

Associates the key with the value. If the key
already exists, the old associated value is
replaced with the new value.

remove(key)

If the key exists, remove the entry and return the
associated value. Returns None otherwise.

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.

last_entry()

Returns the last/largest/right-most entry.
Returns None if no such entry exists.

poll_first_entry()

Removes and returns the first entry.
Returns None if no such entry exists.

poll_last_entry()

Removes and returns the last entry.
Returns None if no such entry exists.

lower_entry(key)

Returns the entry with the greatest key
less than the specified key.
Returns None if no such entry exists.

lower_key(key)

Returns the greatest key less than
the specified key.
Returns None if no such key exists.

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.

floor_key(key)

Returns the greatest key less than or equal
to the specified key.
Returns None if no such key exists.

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.

ceiling_key(key)

Returns the smallest key
greater than or equal to the specified key.
Returns None if no such key exists.

higher_entry(key)

Returns the entry with the smallest key
greater than the specified key.
Returns None if no such entry exists.

higher_key(key)

Returns the smallest key greater than
the specified key.
Returns None if no such key exists.

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.

navigable_key_set()

Same as key_set().

descending_key_set()

Similar to key_set() but the
iteration goes from the largest to the smallest key.

values()

Returns a collection view of the values in the map.
The object returned is iterable.

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.

descending_map()

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.

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.

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.

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 and b, return a negative int if a < b, return a positive int if a > b, and return 0 if a == 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. Returns False 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. Unlike get(), a KeyError 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. Unlike put(), a KeyError 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

Entry

last_entry()

Returns the last/largest/right-most entry. Returns None if no such entry exists.

Return type

Entry

poll_first_entry()

Removes and returns the first entry. Returns None if no such entry exists.

Return type

Entry

poll_last_entry()

Removes and returns the last entry. Returns None if no such entry exists.

Return type

Entry

lower_entry(key)

Returns the entry with the greatest key less than the specified key. Returns None if no such entry exists.

Return type

Entry

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

Entry

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

Entry

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

Entry

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

NavigableSet

navigable_key_set()

Same as key_set().

Return type

NavigableSet

descending_key_set()

Similar to key_set() but the iteration goes from the largest to the smallest key.

Return type

NavigableSet

values()

Returns a collection view of the values in the map. The object returned is iterable.

Return type

Values

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

EntrySet

descending_map()

Returns a reverse order view of the map.

Return type

NavigableMap

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

NavigableMap

Raises
  • TypeError – If from_key or to_key is not comparable to the keys currently in the map.

  • KeyError – If from_key is greater than to_key; or if this map itself has a restricted range, and from_key or to_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.