|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object![]()
![]()
![]()
java.util.AbstractMap
![]()
![]()
<K,V>
java.util.TreeMap<K,V>
, Cloneable
, Map
<K,V>, SortedMap
<K,V>public class TreeMap<K,V>
<K,V>
<K,V>, Cloneable
, Serializable

Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.
Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.
Note that this implementation is not synchronized. If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with an existing key is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
Map m = Collections.synchronizedMap(new TreeMap(...));
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator throws a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
This class is a member of the Java Collections Framework.
Map
,
HashMap
,
Hashtable
,
Comparable
,
Comparator
,
Collection
,
Collections.synchronizedMap(Map)
,
Serialized Form| Constructor Summary | |
|---|---|
TreeMap
Constructs a new, empty map, sorted according to the keys' natural order. |
|
TreeMap
Constructs a new, empty map, sorted according to the given comparator. |
|
TreeMap
Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order. |
|
TreeMap
Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering. |
|
| Method Summary | |
|---|---|
void |
clear
Removes all mappings from this TreeMap. |
Object |
clone
Returns a shallow copy of this TreeMap instance. |
Comparator |
comparator
Returns the comparator used to order this map, or null if this map uses its keys' natural order. |
boolean |
containsKey
Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue
Returns true if this map maps one or more keys to the specified value. |
Set |
entrySet
Returns a set view of the mappings contained in this map. |
K |
firstKey
Returns the first (lowest) key currently in this sorted map. |
V |
get
Returns the value to which this map maps the specified key. |
SortedMap |
headMap
Returns a view of the portion of this map whose keys are strictly less than toKey. |
Set |
keySet
Returns a Set view of the keys contained in this map. |
K |
lastKey
Returns the last (highest) key currently in this sorted map. |
V |
put
Associates the specified value with the specified key in this map. |
void |
putAll
Copies all of the mappings from the specified map to this map. |
V |
remove
Removes the mapping for this key from this TreeMap if present. |
int |
size
Returns the number of key-value mappings in this map. |
SortedMap |
subMap
Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. |
SortedMap |
tailMap
Returns a view of the portion of this map whose keys are greater than or equal to fromKey. |
Collection |
values
Returns a collection view of the values contained in this map. |
Methods inherited from class java.util.AbstractMap ![]() |
|---|
equals |
Methods inherited from class java.lang.Object ![]() |
|---|
finalize |
Methods inherited from interface java.util.Map ![]() |
|---|
equals |
| Constructor Detail |
|---|

public TreeMap()
Comparable


public TreeMap(Comparator![]()
![]()
<? super K> c)
c - the comparator that will be used to sort this map. A
null value indicates that the keys' natural
ordering should be used.

public TreeMap(Map![]()
![]()
<? extends K,? extends V> m)
m - the map whose mappings are to be placed in this map.
ClassCastException

- the keys in t are not Comparable, or
are not mutually comparable.
NullPointerException

- if the specified map is null.

public TreeMap(SortedMap![]()
![]()
<K,? extends V> m)
m - the sorted map whose mappings are to be placed in this map,
and whose comparator is to be used to sort this map.
NullPointerException

- if the specified sorted map is null.| Method Detail |
|---|

public int size()
size

in interface Map
<K,V>size

in class AbstractMap
<K,V>

public boolean containsKey(Object![]()
![]()
key)
containsKey

in interface Map
<K,V>containsKey

in class AbstractMap
<K,V>key - key whose presence in this map is to be tested.
ClassCastException

- if the key cannot be compared with the keys
currently in the map.
NullPointerException

- key is null and this map uses
natural ordering, or its comparator does not tolerate
null keys.

public boolean containsValue(Object![]()
![]()
value)
containsValue

in interface Map
<K,V>containsValue

in class AbstractMap
<K,V>value - value whose presence in this Map is to be tested.

public V get(Object![]()
![]()
key)
get

in interface Map
<K,V>get

in class AbstractMap
<K,V>key - key whose associated value is to be returned.
ClassCastException

- key cannot be compared with the keys
currently in the map.
NullPointerException

- key is null and this map uses
natural ordering, or its comparator does not tolerate
null keys.containsKey(Object)


public Comparator![]()
![]()
<? super K> comparator()
comparator

in interface SortedMap
<K,V>

public K firstKey()
firstKey

in interface SortedMap
<K,V>NoSuchElementException

- Map is empty.

public K lastKey()
lastKey

in interface SortedMap
<K,V>NoSuchElementException

- Map is empty.

public void putAll(Map![]()
![]()
<? extends K,? extends V> map)
putAll

in interface Map
<K,V>putAll

in class AbstractMap
<K,V>map - mappings to be stored in this map.
ClassCastException

- class of a key or value in the specified
map prevents it from being stored in this map.
NullPointerException

- if the given map is null or
this map does not permit null keys and a
key in the specified map is null.

public V put(K key,
V value)
put

in interface Map
<K,V>put

in class AbstractMap
<K,V>key - key with which the specified value is to be associated.value - value to be associated with the specified key.
ClassCastException

- key cannot be compared with the keys
currently in the map.
NullPointerException

- key is null and this map uses
natural order, or its comparator does not tolerate
null keys.

public V remove(Object![]()
![]()
key)
remove

in interface Map
<K,V>remove

in class AbstractMap
<K,V>key - key for which mapping should be removed
ClassCastException

- key cannot be compared with the keys
currently in the map.
NullPointerException

- key is null and this map uses
natural order, or its comparator does not tolerate
null keys.

public void clear()
clear

in interface Map
<K,V>clear

in class AbstractMap
<K,V>

public Object![]()
![]()
clone()
clone

in class AbstractMap
<K,V>Cloneable


public Set![]()
![]()
<K> keySet()
keySet

in interface Map
<K,V>keySet

in class AbstractMap
<K,V>

public Collection![]()