java.util
public class TreeMap<K,V> extends AbstractMap<K,V> implements SortedMap<K,V>, Cloneable, Serializable
This implementation is not synchronized. If you need to share this between
multiple threads, do something like:
SortedMap m
= Collections.synchronizedSortedMap(new TreeMap(...));
The iterators are fail-fast, meaning that any structural
modification, except for remove()
called on the iterator
itself, cause the iterator to throw a
ConcurrentModificationException
rather than exhibit
non-deterministic behavior.
Since: 1.2
See Also: Map HashMap Hashtable LinkedHashMap Comparable Comparator Collection synchronizedSortedMap
UNKNOWN: updated to 1.4
Constructor Summary | |
---|---|
TreeMap()
Instantiate a new TreeMap with no elements, using the keys' natural
ordering to sort. | |
TreeMap(Comparator<? super K> c)
Instantiate a new TreeMap with no elements, using the provided comparator
to sort. | |
TreeMap(Map<? extends K,? extends V> map)
Instantiate a new TreeMap, initializing it with all of the elements in
the provided Map. | |
TreeMap(SortedMap<K,? extends V> sm)
Instantiate a new TreeMap, initializing it with all of the elements in
the provided SortedMap. |
Method Summary | |
---|---|
void | clear()
Clears the Map so it has no keys. |
Object | clone()
Returns a shallow clone of this TreeMap. |
Comparator<? super K> | comparator()
Return the comparator used to sort this map, or null if it is by
natural order.
|
boolean | containsKey(Object key)
Returns true if the map contains a mapping for the given key.
|
boolean | containsValue(Object value)
Returns true if the map contains at least one mapping to the given value.
|
Set<Entry<K,V>> | entrySet()
Returns a "set view" of this TreeMap's entries. |
K | firstKey()
Returns the first (lowest) key in the map.
|
V | get(Object key)
Return the value in this TreeMap associated with the supplied key,
or null if the key maps to nothing. |
SortedMap<K,V> | headMap(K toKey)
Returns a view of this Map including all entries with keys less than
toKey . |
Set<K> | keySet()
Returns a "set view" of this TreeMap's keys. |
K | lastKey()
Returns the last (highest) key in the map.
|
V | put(K key, V value)
Puts the supplied value into the Map, mapped by the supplied key.
|
void | putAll(Map<? extends K,? extends V> m)
Copies all elements of the given map into this TreeMap. |
V | remove(Object key)
Removes from the TreeMap and returns the value which is mapped by the
supplied key. |
int | size()
Returns the number of key-value mappings currently in this Map.
|
SortedMap<K,V> | subMap(K fromKey, K toKey)
Returns a view of this Map including all entries with keys greater or
equal to fromKey and less than toKey (a
half-open interval). |
SortedMap<K,V> | tailMap(K fromKey)
Returns a view of this Map including all entries with keys greater or
equal to fromKey . |
Collection<V> | values()
Returns a "collection view" (or "bag view") of this TreeMap's values.
|
See Also: Comparable
Parameters: c the sort order for the keys of this map, or null for the natural order
Parameters: map a Map, whose entries will be put into this TreeMap
Throws: ClassCastException if the keys in the provided Map are not comparable NullPointerException if map is null
See Also: Comparable
Parameters: sm a SortedMap, whose entries will be put into this TreeMap
Throws: NullPointerException if sm is null
Returns: the clone
Returns: the map's comparator
Parameters: key the key to look for
Returns: true if the key has a mapping
Throws: ClassCastException if key is not comparable to map elements NullPointerException if key is null and the comparator is not tolerant of nulls
Parameters: value the value to look for
Returns: true if the value appears in a mapping
Note that the iterators for all three views, from keySet(), entrySet(), and values(), traverse the TreeMap in sorted sequence.
Returns: a set view of the entries
Returns: the first key
Throws: NoSuchElementException if the map is empty
null
if the key maps to nothing. NOTE: Since the value
could also be null, you must use containsKey to see if this key
actually maps to something.
Parameters: key the key for which to fetch an associated value
Returns: what the key maps to, if present
Throws: ClassCastException if key is not comparable to elements in the map NullPointerException if key is null but the comparator does not tolerate nulls
See Also: TreeMap containsKey
toKey
. The returned map is backed by the original, so changes
in one appear in the other. The submap will throw an
{@link IllegalArgumentException} for any attempt to access or add an
element beyond the specified cutoff. The returned map does not include
the endpoint; if you want inclusion, pass the successor element.
Parameters: toKey the (exclusive) cutoff point
Returns: a view of the map less than the cutoff
Throws: ClassCastException if toKey
is not compatible with
the comparator (or is not Comparable, for natural ordering) NullPointerException if toKey is null, but the comparator does not
tolerate null elements
Returns: a set view of the keys
Returns: the last key
Throws: NoSuchElementException if the map is empty
equals()
this key. NOTE: Since the prior value could also be null, you must
first use containsKey if you want to see if you are replacing the
key's mapping.
Parameters: key the key used to locate the value value the value to be stored in the Map
Returns: the prior mapping of the key, or null if there was none
Throws: ClassCastException if key is not comparable to current map keys NullPointerException if key is null, but the comparator does not tolerate nulls
Parameters: m the map to be added
Throws: ClassCastException if a key in m is not comparable with keys in the map NullPointerException if a key in m is null, and the comparator does not tolerate nulls
null
is returned. NOTE: Since the value
could also be null, you must use containsKey to see if you are
actually removing a mapping.
Parameters: key the key used to locate the value to remove
Returns: whatever the key mapped to, if present
Throws: ClassCastException if key is not comparable to current map keys NullPointerException if key is null, but the comparator does not tolerate nulls
Returns: the size
fromKey
and less than toKey
(a
half-open interval). The returned map is backed by the original, so
changes in one appear in the other. The submap will throw an
{@link IllegalArgumentException} for any attempt to access or add an
element beyond the specified cutoffs. The returned map includes the low
endpoint but not the high; if you want to reverse this behavior on
either end, pass in the successor element.
Parameters: fromKey the (inclusive) low cutoff point toKey the (exclusive) high cutoff point
Returns: a view of the map between the cutoffs
Throws: ClassCastException if either cutoff is not compatible with the comparator (or is not Comparable, for natural ordering) NullPointerException if fromKey or toKey is null, but the comparator does not tolerate null elements IllegalArgumentException if fromKey is greater than toKey
fromKey
. The returned map is backed by the
original, so changes in one appear in the other. The submap will throw an
{@link IllegalArgumentException} for any attempt to access or add an
element beyond the specified cutoff. The returned map includes the
endpoint; if you want to exclude it, pass in the successor element.
Parameters: fromKey the (inclusive) low cutoff point
Returns: a view of the map above the cutoff
Throws: ClassCastException if fromKey
is not compatible with
the comparator (or is not Comparable, for natural ordering) NullPointerException if fromKey is null, but the comparator
does not tolerate null elements