All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.sun.java.util.collections.TreeMap

java.lang.Object
   |
   +----com.sun.java.util.collections.AbstractMap
           |
           +----com.sun.java.util.collections.TreeMap

public class TreeMap
extends AbstractMap
implements SortedMap, 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 Class (see Comparable), or by the Comparator provided at TreeMap 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 Corman, Leiserson, and Rivest's Introduction to Algorithms.

Note that the ordering maintained by a TreeMap (whether or not an explicit Comparator is provided) must be total if the TreeMap is to correctly implement the Map interface. (See Comparable or Comparator for a definition of total ordering.) This is so because the Map interface is defined in terms of the equals operation, but a TreeMap 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 TreeMap, equal. The behavior of a TreeMap is well-defined even if its ordering is strictly partial; it just fails to obey the general contract of the Map interface.

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.synchronizedMap 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, Hashtable, Comparable, Comparator, Collection, synchronizedMap

Constructor Index

 o TreeMap()
Constructs a new, empty TreeMap, sorted according to the keys' natural order.
 o TreeMap(Comparator)
Constructs a new, empty TreeMap, sorted according to the given comparator.
 o TreeMap(Map)
Constructs a new TreeMap containing the same mappings as the given Map, sorted according to the keys' natural order.
 o TreeMap(SortedMap)
Constructs a new TreeMap containing the same mappings as the given SortedMap, sorted according to the same ordering.

Method Index

 o addAllForTreeSet(SortedSet, Object)
Intended to be called only from TreeSet.addAll
 o clear()
Removes all mappings from this TreeMap.
 o clone()
Returns a shallow copy of this TreeMap.
 o comparator()
Returns the comparator used to order this TreeMap, or null if this TreeMap uses its keys' natural order.
 o containsKey(Object)
Returns true if this TreeMap contains a mapping for the specified key.
 o containsValue(Object)
Returns true if this Map maps one or more keys to the specified value.
 o entrySet()
Returns a Set view of the mappings contained in this Map.
 o firstKey()
Returns the first (lowest) key currently in this SortedMap.
 o get(Object)
Returns the value to which this TreeMap maps the specified key.
 o headMap(Object)
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(Object, Object)
Associates the specified value with the specified key in this TreeMap.
 o putAll(Map)
Copies all of the mappings from the specified Map to this TreeMap These mappings will replace any mappings that this TreeMap had for any of the keys currently in the specified Map.
 o readTreeSet(int, ObjectInputStream, Object)
Intended to be called only from TreeSet.readObject
 o remove(Object)
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(Object, Object)
Returns a view of the portion of this TreeMap whose keys range from fromKey, inclusive, to toKey, exclusive.
 o tailMap(Object)
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.

Constructors

 o TreeMap
 public TreeMap()
Constructs a new, empty TreeMap, sorted according to the keys' natural order. 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 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 m)
Constructs a new TreeMap containing the same mappings as the given Map, sorted according to the keys' natural order. 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. This method runs in n*log(n) time.

Throws: ClassCastException
the keys in t are not Comparable, or are not mutually comparable.
 o TreeMap
 public TreeMap(SortedMap m)
Constructs a new TreeMap containing the same mappings as the given SortedMap, sorted according to the same ordering. This method runs in linear time.

Methods

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

Returns:
the number of key-value mappings in this TreeMap.
Overrides:
size in class AbstractMap
 o containsKey
 public boolean containsKey(Object 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.
Returns:
true if this TreeMap contains a mapping for the specified key.
Throws: ClassCastException
key cannot be compared with the keys currently in the TreeMap.
Throws: NullPointerException
key is null and this TreeMap uses natural ordering, or its comparator does not tolerate null keys.
Overrides:
containsKey in class AbstractMap
 o containsValue
 public boolean containsValue(Object value)
Returns true if this Map maps one or more keys to the specified value. More formally, returns true if and only if this Map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the Map size for most implementations of Map.

Parameters:
value - value whose presence in this Map is to be tested.
Overrides:
containsValue in class AbstractMap
 o get
 public Object get(Object 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.
Returns:
the value to which this TreeMap maps the specified key, or null if the TreeMap contains no mapping for the key.
Throws: ClassCastException
key cannot be compared with the keys currently in the TreeMap.
Throws: NullPointerException
key is null and this TreeMap uses natural ordering, or its comparator does not tolerate null keys.
Overrides:
get in class AbstractMap
See Also:
containsKey
 o comparator
 public Comparator comparator()
Returns the comparator used to order this TreeMap, or null if this TreeMap uses its keys' natural order.

Returns:
the Comparator associated with this SortedMap, or null if it uses its keys' natural sort method.
 o firstKey
 public Object 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 Object 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 putAll
 public void putAll(Map map)
Copies all of the mappings from the specified Map to this TreeMap These mappings will replace any mappings that this TreeMap had for any of the keys currently in the specified Map.

Parameters:
t - Mappings to be stored in this Map.
Throws: ClassCastException
class of a key or value in the specified Map prevents it from being stored in this Map.
Throws: NullPointerException
this Map does not permit null keys or values, and the specified key or value is null.
Overrides:
putAll in class AbstractMap
 o put
 public Object put(Object key,
                   Object 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.
Throws: NullPointerException
key is null and this TreeMap uses natural order, or its comparator does not tolerate null keys.
Overrides:
put in class AbstractMap
 o remove
 public Object remove(Object 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.
Throws: NullPointerException
key is null and this TreeMap uses natural order, 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 TreeMap. (The keys and values themselves are not cloned.)

Returns:
a shallow copy of this TreeMap.
Overrides:
clone in class Object
 o keySet
 public Set 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.

Returns:
a Set view of the keys contained in this TreeMap.
Overrides:
keySet in class AbstractMap
 o values
 public Collection 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.

Returns:
a Collection view of the values contained in this TreeMap.
Overrides:
values in class AbstractMap
 o entrySet
 public Set entrySet()
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.

Returns:
a Set view of the mappings contained in this Map.
Overrides:
entrySet in class AbstractMap
See Also:
Entry
 o subMap
 public SortedMap subMap(Object fromKey,
                         Object 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.

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.

Note: this method always returns a half-open range (which includes its low endpoint but not its high endpoint). If you need a closed range (which includes both endpoints), and the key type allows for calculation of the successor a given key, merely request the subrange from lowEndpoint to successor(highEndpoint). For example, suppose that m is a Map whose keys are Strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, inclusive:

    Map sub = m.subMap(low, high+"\0");
A similarly technique can be used to generate an open range (which contains neither endpoint). The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, exclusive:
    Map sub = m.subMap(low+"\0", high);

Parameters:
fromKey - low endpoint (inclusive) of the subMap.
toKey - high endpoint (exclusive) of the subMap.
Returns:
a view of the portion of this TreeMap whose keys range from fromKey, inclusive, to toKey, exclusive.
Throws: NullPointerException
fromKey or toKey is null and this TreeMap uses natural order, or its comparator does not tolerate null keys.
Throws: IllegalArgumentException
fromKey is greater than toKey.
 o headMap
 public SortedMap headMap(Object 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.

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

Note: this method always returns a view that does not contain its (high) endpoint. If you need a view that does contain this endpoint, and the key type allows for calculation of the successor a given key, merely request a headMap bounded by successor(highEndpoint). For example, suppose that suppose that m is a Map whose keys are Strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are less than or equal to high:

    Map head = m.headMap(high+"\0");

Parameters:
toKey - high endpoint (exclusive) of the headMap.
Returns:
a view of the portion of this TreeMap whose keys are strictly less than toKey.
Throws: NullPointerException
toKey is null and this TreeMap uses natural order, or its comparator does not tolerate null keys.
 o tailMap
 public SortedMap tailMap(Object 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.

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

Note: this method always returns a view that contains its (low) endpoint. If you need a view that does not contain this endpoint, and the element type allows for calculation of the successor a given value, merely request a tailMap bounded by successor(lowEndpoint). For For example, suppose that suppose that m is a Map whose keys are Strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are strictly greater than low:

    Map tail = m.tailMap(low+"\0");

Parameters:
fromKey - low endpoint (inclusive) of the tailMap.
Returns:
a view of the portion of this TreeMap whose keys are strictly less than toKey.
Throws: NullPointerException
fromKey is null and this TreeMap uses natural ordering, or its comparator does not tolerate null keys.
 o readTreeSet
 void readTreeSet(int size,
                  ObjectInputStream s,
                  Object defaultVal) throws IOException, ClassNotFoundException
Intended to be called only from TreeSet.readObject

 o addAllForTreeSet
 void addAllForTreeSet(SortedSet set,
                       Object defaultVal)
Intended to be called only from TreeSet.addAll


All Packages  Class Hierarchy  This Package  Previous  Next  Index