[all packages] [package java.util] [class hierarchy] [index]

public class java.util.TreeMap<A, B>

(source file: TreeMap.java)
java.lang.Object
   |
   +----java.util.AbstractMap<A, B>
           |
           +----java.util.TreeMap<A, B>

The pure class interface.
public class TreeMap<A, B>
  extends AbstractMap<A, B>
  implements SortedMap<A, B>, Cloneable, java.io.Serializable
Red-Black tree based implementation of the Map interface. This class guarantees that the Map will be in ascending key order, sorted according to the natural sort method for the key Class (see Comparable), or by the Comparator provided at TreeMap creation time, depending on which constructor is used. Note that this ordering must be total in order for the Tree to function properly. (A total ordering is an ordering for which a.compareTo(b)==0 implies that a.equals(b).)

This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Corman, Leiserson, and Rivest's Introduction to Algorithms.

Note that this implementation is not synchronized. If multiple threads access a TreeMap concurrently, and at least one of the threads modifies the TreeMap 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 a key that is already contained in the Table is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the TreeMap. If no such object exists, the TreeMap should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the TreeMap:

	Map m = Collections.synchronizedMap(new TreeMap(...));
 

The Iterators returned by the iterator methods of the Collections returned by all of TreeMap's "collection view methods" are fail-fast: if the TreeMap 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 will throw 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.

See also:
Map, HashMap, ArrayMap, Hashtable, Comparable, Comparator, Collection, synchronizedMap()

Constuctor Index

O TreeMap(Map<A, B>)
Constructs a new TreeMap containing the same mappings as the given Map, sorted according
O TreeMap(SortedMap<A, B>)
Constructs a new TreeMap containing the same mappings as the given SortedMap, sorted
O TreeMap(Comparator<A>)
Constructs a new, empty TreeMap, sorted according to the given comparator
O TreeMap()
Constructs a new, empty TreeMap, sorted according to the keys' natural sort method

Methods

O clear()
Removes all mappings from this TreeMap.
O clone()
Returns a shallow copy of this TreeSet
O comparator()
Returns the comparator used to order this TreeMap, or null if this TreeMap uses its keys
O containsKey(A)
Returns true if this TreeMap contains a mapping for the specified key.
O entries()
Returns a Set view of the mappings contained in this Map
O firstKey()
Returns the first (lowest) key currently in this SortedMap.
O get(A)
Returns the value to which this TreeMap maps the specified key. Returns null if the
O headMap(A)
Returns a view of the portion of this TreeMap whose keys are strictly less than toKey
O keySet()
Returns a Set view of the keys contained in this TreeMap
O lastKey()
Returns the last (highest) key currently in this SortedMap.
O put(A, B)
Associates the specified value with the specified key in this TreeMap. If the TreeMap
O remove(A)
Removes the mapping for this key from this TreeMap if present.
O size()
Returns the number of key-value mappings in this TreeMap.
O subMap(A, A)
Returns a view of the portion of this TreeMap whose keys range from fromKey, inclusive,
O tailMap(A)
Returns a view of the portion of this TreeMap whose keys are strictly less than toKey
O values()
Returns a Collection view of the values contained in this TreeMap. The Collection's

Inner Class Index

Entry<A, B>
Node in the Tree
O Iterator
TreeMap Iterator.
O SubMap

Constructors

O TreeMap
public TreeMap();
Constructs a new, empty TreeMap, sorted according to the keys' natural sort method. All keys inserted into the TreeMap must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a typeMismatchException for any elements k1 and k2 in the TreeMap. If the user attempts to put a key into the TreeMap that violates this constraint (for example, the user attempts to put a String key into a TreeMap whose keys are Integers), the put(Object key, Object value) call will throw a ClassCastException.

See also:
Comparable

O TreeMap

public TreeMap(Comparator<A> c);
Constructs a new, empty TreeMap, sorted according to the given comparator. All keys inserted into the TreeMap must be mutually comparable by the given comparator: comparator.compare(k1, k2) must not throw a typeMismatchException for any keys k1 and k2 in the TreeMap. If the user attempts to put a key into the TreeMap that violates this constraint, the put(Object key, Object value) call will throw a ClassCastException.

O TreeMap

public TreeMap(Map<A, B> m);
Constructs a new TreeMap containing the same mappings as the given Map, sorted according to the keys' natural sort method. All keys inserted into the TreeMap must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a typeMismatchException for any elements k1 and k2 in the TreeMap.

Throws:
ClassCastException -the keys in t are not Comparable, or are not mutually comparable.

O TreeMap

public TreeMap(SortedMap<A, B> m);
Constructs a new TreeMap containing the same mappings as the given SortedMap, sorted according to the same ordering.

Methods

O size
public int size();
Returns the number of key-value mappings in this TreeMap.

Overrides:
size in class AbstractMap

O containsKey

public boolean containsKey(A key);
Returns true if this TreeMap contains a mapping for the specified key.

Parameters:
key - key whose presence in this Map is to be tested.
Throws:
ClassCastException -key cannot be compared with the keys currently in the TreeMap.
NullPointerException -key is null and this TreeMap uses natural sort method, or its comparator does not tolerate null keys.
Overrides:
containsKey in class AbstractMap

O get

public B get(A key);
Returns the value to which this TreeMap maps the specified key. Returns null if the TreeMap contains no mapping for this key. A return value of null does not necessarily indicate that the TreeMap contains no mapping for the key; it's also possible that the TreeMap explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Parameters:
key - key whose associated value is to be returned.
Throws:
ClassCastException -key cannot be compared with the keys currently in the TreeMap.
NullPointerException -key is null and this TreeMap uses natural sort method, or its comparator does not tolerate null keys.
Overrides:
get in class AbstractMap
See also:
containsKey(A)

O comparator

public Comparator<A> comparator();
Returns the comparator used to order this TreeMap, or null if this TreeMap uses its keys natural sort method.

Returns:
the Comparator associated with this SortedMap, or null if it uses its keys' natural sort method.

O firstKey

public A firstKey();
Returns the first (lowest) key currently in this SortedMap.

Returns:
the first (lowest) key currently in this SortedMap.
Throws:
NoSuchElementException -Map is empty.

O lastKey

public A lastKey();
Returns the last (highest) key currently in this SortedMap.

Returns:
the last (highest) key currently in this SortedMap.
Throws:
NoSuchElementException -Map is empty.

O put

public B put(A key,
             B value);
Associates the specified value with the specified key in this TreeMap. If the TreeMap previously contained a mapping for this key, the old value is replaced.

Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the TreeMap previously associated null with the specified key.
Throws:
ClassCastException -key cannot be compared with the keys currently in the TreeMap.
NullPointerException -key is null and this TreeMap uses natural sort method, or its comparator does not tolerate null keys.
Overrides:
put in class AbstractMap

O remove

public B remove(A key);
Removes the mapping for this key from this TreeMap if present.

Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the TreeMap previously associated null with the specified key.
Throws:
ClassCastException -key cannot be compared with the keys currently in the TreeMap.
NullPointerException -key is null and this TreeMap uses natural sort method, or its comparator does not tolerate null keys.
Overrides:
remove in class AbstractMap

O clear

public void clear();
Removes all mappings from this TreeMap.

Overrides:
clear in class AbstractMap

O clone

public Object clone();
Returns a shallow copy of this TreeSet. (The elements themselves are not cloned.)

Overrides:
clone in class Object

O keySet

public Set<A> keySet();
Returns a Set view of the keys contained in this TreeMap. The Set's Iterator will return the keys in ascending order. The Set is backed by the TreeMap, so changes to the TreeMap are reflected in the Set, and vice-versa. The Set supports element removal, which removes the corresponding mapping from the TreeMap, via the Iterator.remove, Set.remove, removeAll retainAll, and clear operations. It does not support the add or addAll operations.

Overrides:
keySet in class AbstractMap

O values

public Collection<B> values();
Returns a Collection view of the values contained in this TreeMap. The Collection's iterator will return the values in the order that their corresponding keys appear in the tree. The Collection is backed by the TreeMap, so changes to the TreeMap are reflected in the Collection, and vice-versa. The Collection supports element removal, which removes the corresponding mapping from the TreeMap, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Overrides:
values in class AbstractMap

O entries

public Set<Entry<A, B>> entries();
Returns a Set view of the mappings contained in this Map. The Set's Iterator will return the mappings in ascending Key order. Each element in the returned set is a Map.Entry. The Set is backed by the TreeMap, so changes to the TreeMap are reflected in the Set, and vice-versa. The Set supports element removal, which removes the corresponding mapping from the TreeMap, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Overrides:
entries in class AbstractMap
See also:
Map.Entry

O subMap

public SortedMap<A, B> subMap(A fromKey,
                              A toKey);
Returns a view of the portion of this TreeMap whose keys range from fromKey, inclusive, to toKey, exclusive. The returned Map is backed by this TreeMap, so changes in the returned Map are reflected in this TreeMap, and vice-versa. The returned Map supports all optional Map operations. It does not support the subMap operation, as it is not a TreeMap.

The Map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key less than fromKey or greater than or equal to toKey.

Parameters:
fromKey - low endpoint (inclusive) of the subMap.
toKey - high endpoint (exclusive) of the subMap.
Throws:
ClassCastException -fromKey or toKey cannot be compared with the keys currently in the TreeMap.
NullPointerException -fromKey or toKey is null and this TreeMap uses natural sort method, or its comparator does not tolerate null keys.
IllegalArgumentException -fromKey is greater than toKey.

O headMap

public SortedMap<A, B> headMap(A toKey);
Returns a view of the portion of this TreeMap whose keys are strictly less than toKey. The returned Map is backed by this TreeMap, so changes in the returned Map are reflected in this TreeMap, and vice-versa. The returned Map supports all optional Map operations. It does not support the subMap operation, as it is not a TreeMap.

The Map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key greater than or equal to toKey.

Parameters:
toKey - high endpoint (exclusive) of the headMap.
Throws:
ClassCastException -toKey cannot be compared with the keys currently in the TreeMap.
NullPointerException -toKey is null and this TreeMap uses natural sort method, or its comparator does not tolerate null keys.

O tailMap

public SortedMap<A, B> tailMap(A fromKey);
Returns a view of the portion of this TreeMap whose keys are strictly less than toKey. The returned Map is backed by this TreeMap, so changes in the returned Map are reflected in this TreeMap, and vice-versa. The returned Map supports all optional Map operations. It does not support the subMap operation, as it is not a TreeMap.

The Map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key greater than or equal to toKey.

Parameters:
toKey - high endpoint (exclusive) of the headMap.
Throws:
ClassCastException -toKey cannot be compared with the keys currently in the TreeMap.
NullPointerException -toKey is null and this TreeMap uses natural sort method, or its comparator does not tolerate null keys.

Inner Classes

O private class SubMap
private class SubMap
  extends AbstractMap<A, B>
  implements SortedMap<A, B>, java.io.Serializable

O private class Iterator

private class Iterator
  implements java.util.Iterator<A>
TreeMap Iterator.

static class Entry<A, B>

static class Entry<A, B>
  implements java.util.Map.Entry<A, B>
Node in the Tree. Doubles as a means to pass key-value pairs back to user (see Map.Entry).


[all packages] [package java.util] [class hierarchy] [index]
java.util.TreeMap.html