java::util::TreeMap Class Reference

Inheritance diagram for java::util::TreeMap:

Inheritance graph
java::util::AbstractMapjava::util::Mapjava::util::SortedMapjava::lang::Interfacejava::lang::Object
[legend]
Collaboration diagram for java::util::TreeMap:

Collaboration graph
java::util::AbstractMapjava::util::Mapjava::util::SortedMapjava::lang::Interfacejava::lang::Comparablejava::lang::Objectjava::lang::ObjectRef
[legend]

List of all members.


Detailed Description

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.

Author:
Josh Bloch and Doug Lea
Version:
1.54, 12/03/01
See also:
Map

HashMap

Hashtable

Comparable

Comparator

Collection

Collections::synchronizedMap(Map)

Since:
1.2

Public Member Functions

 TreeMap (const Ref< Comparator > &c=0)
 Constructs a new, empty map, sorted according to the given comparator.
 TreeMap (const Map &m)
 Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order.
 TreeMap (const SortedMap &m)
 Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering.
virtual jint size () const
 Returns the number of elements in this collection.
virtual jboolean isEmpty () const
 Returns true if this collection contains no elements.
virtual jboolean containsKey (const ObjectRef &key) const
 Returns true if this map contains a mapping for the specified key.
virtual jboolean containsValue (const ObjectRef &value) const
 Returns true if this map maps one or more keys to the specified value.
virtual ObjectRef get (const ObjectRef &key) const
 Returns the value to which this map maps the specified key.
virtual ObjectRef put (const ObjectRef &key, const ObjectRef &value)
 Associates the specified value with the specified key in this map (optional operation).
virtual ObjectRef remove (const ObjectRef &key)
 Removes the mapping for this key from this map if it is present (optional operation).
virtual void putAll (const Map &t)
 Copies all of the mappings from the specified map to this map (optional operation).
virtual void clear ()
 Removes all mappings from this map (optional operation).
virtual Ref< SetkeySet () const
 Returns a set view of the keys contained in this map.
virtual Ref< Collectionvalues () const
 Returns a collection view of the values contained in this map.
virtual Ref< SetentrySet () const
 Returns a set view of the mappings contained in this map.
virtual Ref< Comparatorcomparator () const
 Returns the comparator associated with this sorted map, or null if it uses its keys' natural ordering.
virtual Ref< SortedMapsubMap (const ObjectRef &fromKey, const ObjectRef &toKey) const
 Returns a view of the portion of this sorted map whose keys range from fromKey, inclusive, to toKey, exclusive.
virtual Ref< SortedMapheadMap (const ObjectRef &toKey) const
 Returns a view of the portion of this sorted map whose keys are strictly less than toKey.
virtual Ref< SortedMaptailMap (const ObjectRef &fromKey) const
 Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey.
virtual ObjectRef firstKey () const
 Returns the first (lowest) key currently in this sorted map.
virtual ObjectRef lastKey () const
 Returns the last (highest) key currently in this sorted map.
virtual jboolean equals (const Object &o) const
 Compares the specified object with this map for equality.
virtual jint hashCode () const
 Returns the hash code value for this map.
virtual String toString () const
 Returns a string representation of the object.

Constructor & Destructor Documentation

java::util::TreeMap::TreeMap ( const Ref< Comparator > &  c = 0  ) 

Constructs a new, empty map, sorted according to the given comparator.

All keys inserted into the map must be mutually comparable by the given comparator: comparator.compare(k1, k2) must not throw a ClassCastException for any keys k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint, the put(Object key, Object value) call will throw a ClassCastException.

Parameters:
c the comparator that will be used to sort this map. A null value indicates that the keys' natural ordering should be used.

java::util::TreeMap::TreeMap ( const Map m  ) 

Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order.

All keys inserted into the new map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any elements k1 and k2 in the map. This method runs in n*log(n) time.

Parameters:
m the map whose mappings are to be placed in this map.
Exceptions:
ClassCastException the keys in t are not Comparable, or are not mutually comparable.
NullPointerException if the specified map is null.

java::util::TreeMap::TreeMap ( const SortedMap m  ) 

Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering.

This method runs in linear time.

Parameters:
m the sorted map whose mappings are to be placed in this map, and whose comparator is to be used to sort this map.
Exceptions:
NullPointerException if the specified sorted map is null.


Member Function Documentation

virtual jint java::util::TreeMap::size (  )  const [virtual]

Returns the number of elements in this collection.

If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:
the number of elements in this collection

Reimplemented from java::util::AbstractMap.

virtual jboolean java::util::TreeMap::isEmpty (  )  const [virtual]

Returns true if this collection contains no elements.

This implementation returns size() == 0.

Returns:
true if this collection contains no elements.

Reimplemented from java::util::AbstractMap.

virtual jboolean java::util::TreeMap::containsKey ( const ObjectRef key  )  const [virtual]

Returns true if this map contains a mapping for the specified key.

More formally, returns true if and only if this map contains at a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)

Parameters:
key key whose presence in this map is to be tested.
Returns:
true if this map contains a mapping for the specified key.
Exceptions:
ClassCastException if the key is of an inappropriate type for this map (optional).
NullPointerException if the key is null and this map does not not permit null keys (optional).

Reimplemented from java::util::AbstractMap.

virtual jboolean java::util::TreeMap::containsValue ( const ObjectRef value  )  const [virtual]

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.
Returns:
true if a mapping to value exists; false otherwise.
Since:
1.2

Reimplemented from java::util::AbstractMap.

virtual ObjectRef java::util::TreeMap::get ( const ObjectRef key  )  const [virtual]

Returns the value to which this map maps the specified key.

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

More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

Parameters:
key key whose associated value is to be returned.
Returns:
the value to which this map maps the specified key, or null if the map contains no mapping for this key.
Exceptions:
ClassCastException if the key is of an inappropriate type for this map (optional).
NullPointerException key is null and this map does not not permit null keys (optional).
See also:
containsKey(Object)

Reimplemented from java::util::AbstractMap.

virtual ObjectRef java::util::TreeMap::put ( const ObjectRef key,
const ObjectRef value 
) [virtual]

Associates the specified value with the specified key in this map (optional operation).

If the map previously contained a mapping for this key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.))

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 map previously associated null with the specified key, if the implementation supports null values.
Exceptions:
UnsupportedOperationException if the put operation is not supported by this map.
ClassCastException if the class of the specified key or value prevents it from being stored in this map.
IllegalArgumentException if some aspect of this key or value prevents it from being stored in this map.
NullPointerException this map does not permit null keys or values, and the specified key or value is null.

Reimplemented from java::util::AbstractMap.

virtual ObjectRef java::util::TreeMap::remove ( const ObjectRef key  )  [virtual]

Removes the mapping for this key from this map if it is present (optional operation).

More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)

Returns the value to which the map previously associated the key, or null if the map contained no mapping for this key. (A null return can also indicate that the map previously associated null with the specified key if the implementation supports null values.) The map will not contain a mapping for the specified key once the call returns.

Parameters:
key key whose mapping is to be removed from the map.
Returns:
previous value associated with specified key, or null if there was no mapping for key.
Exceptions:
ClassCastException if the key is of an inappropriate type for this map (optional).
NullPointerException if the key is null and this map does not not permit null keys (optional).
UnsupportedOperationException if the remove method is not supported by this map.

Reimplemented from java::util::AbstractMap.

virtual void java::util::TreeMap::putAll ( const Map t  )  [virtual]

Copies all of the mappings from the specified map to this map (optional operation).

The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is unspecified if the specified map is modified while the operation is in progress.

Parameters:
t Mappings to be stored in this map.
Exceptions:
UnsupportedOperationException if the putAll method is not supported by this map.
ClassCastException if the class of a key or value in the specified map prevents it from being stored in this map.
IllegalArgumentException some aspect of a key or value in the specified map prevents it from being stored in this map.
NullPointerException the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or values.

Reimplemented from java::util::AbstractMap.

virtual void java::util::TreeMap::clear (  )  [virtual]

Removes all mappings from this map (optional operation).

Exceptions:
UnsupportedOperationException clear is not supported by this map.

Reimplemented from java::util::AbstractMap.

virtual Ref<Set> java::util::TreeMap::keySet (  )  const [virtual]

Returns a set view of the keys contained in this map.

The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, 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 map.

Reimplemented from java::util::AbstractMap.

virtual Ref<Collection> java::util::TreeMap::values (  )  const [virtual]

Returns a collection view of the values contained in this map.

The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, 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 map.

Reimplemented from java::util::AbstractMap.

virtual Ref<Set> java::util::TreeMap::entrySet (  )  const [virtual]

Returns a set view of the mappings contained in this map.

Each element in the returned set is a Map.Entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, 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.

Implements java::util::Map.

virtual Ref<Comparator> java::util::TreeMap::comparator (  )  const [virtual]

Returns the comparator associated with this sorted map, or null if it uses its keys' natural ordering.

Returns:
the comparator associated with this sorted map, or null if it uses its keys' natural ordering.

Implements java::util::SortedMap.

virtual Ref<SortedMap> java::util::TreeMap::subMap ( const ObjectRef fromKey,
const ObjectRef toKey 
) const [virtual]

Returns a view of the portion of this sorted map whose keys range from fromKey, inclusive, to toKey, exclusive.

(If fromKey and toKey are equal, the returned sorted map is empty.) The returned sorted map is backed by this sorted map, so changes in the returned sorted map are reflected in this sorted map, and vice-versa. The returned Map supports all optional map operations that this sorted map supports.

The map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key outside the specified range.

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+"");

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+"", high);

Parameters:
fromKey low endpoint (inclusive) of the subMap.
toKey high endpoint (exclusive) of the subMap.
Returns:
a view of the specified range within this sorted map.
Exceptions:
ClassCastException if fromKey and toKey cannot be compared to one another using this map's comparator (or, if the map has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception if fromKey or toKey cannot be compared to keys currently in the map.
IllegalArgumentException if fromKey is greater than toKey; or if this map is itself a subMap, headMap, or tailMap, and fromKey or toKey are not within the specified range of the subMap, headMap, or tailMap.
NullPointerException if fromKey or toKey is null and this sorted map does not tolerate null keys.

Implements java::util::SortedMap.

virtual Ref<SortedMap> java::util::TreeMap::headMap ( const ObjectRef toKey  )  const [virtual]

Returns a view of the portion of this sorted map whose keys are strictly less than toKey.

The returned sorted map is backed by this sorted map, so changes in the returned sorted map are reflected in this sorted map, and vice-versa. The returned map supports all optional map operations that this sorted map supports.

The map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key outside the specified range.

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+"");

Parameters:
toKey high endpoint (exclusive) of the subMap.
Returns:
a view of the specified initial range of this sorted map.
Exceptions:
ClassCastException if toKey is not compatible with this map's comparator (or, if the map has no comparator, if toKey does not implement Comparable). Implementations may, but are not required to, throw this exception if toKey cannot be compared to keys currently in the map.
IllegalArgumentException if this map is itself a subMap, headMap, or tailMap, and toKey is not within the specified range of the subMap, headMap, or tailMap.
NullPointerException if toKey is null and this sorted map does not tolerate null keys.

Implements java::util::SortedMap.

virtual Ref<SortedMap> java::util::TreeMap::tailMap ( const ObjectRef fromKey  )  const [virtual]

Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey.

The returned sorted map is backed by this sorted map, so changes in the returned sorted map are reflected in this sorted map, and vice-versa. The returned map supports all optional map operations that this sorted map supports.

The map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key outside the specified range.

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 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+"");

Parameters:
fromKey low endpoint (inclusive) of the tailMap.
Returns:
a view of the specified final range of this sorted map.
Exceptions:
ClassCastException if fromKey is not compatible with this map's comparator (or, if the map has no comparator, if fromKey does not implement Comparable). Implementations may, but are not required to, throw this exception if fromKey cannot be compared to keys currently in the map.
IllegalArgumentException if this map is itself a subMap, headMap, or tailMap, and fromKey is not within the specified range of the subMap, headMap, or tailMap.
NullPointerException if fromKey is null and this sorted map does not tolerate null keys.

Implements java::util::SortedMap.

virtual ObjectRef java::util::TreeMap::firstKey (  )  const [virtual]

Returns the first (lowest) key currently in this sorted map.

Returns:
the first (lowest) key currently in this sorted map.
Exceptions:
NoSuchElementException if this map is empty.

Implements java::util::SortedMap.

virtual ObjectRef java::util::TreeMap::lastKey (  )  const [virtual]

Returns the last (highest) key currently in this sorted map.

Returns:
the last (highest) key currently in this sorted map.
Exceptions:
NoSuchElementException if this map is empty.

Implements java::util::SortedMap.

virtual jboolean java::util::TreeMap::equals ( const Object o  )  const [virtual]

Compares the specified object with this map for equality.

Returns true if the given object is also a map and the two Maps represent the same mappings. More formally, two maps t1 and t2 represent the same mappings if t1.entrySet().equals(t2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.

Parameters:
o object to be compared for equality with this map.
Returns:
true if the specified object is equal to this map.

Reimplemented from java::util::AbstractMap.

virtual jint java::util::TreeMap::hashCode (  )  const [virtual]

Returns the hash code value for this map.

The hash code of a map is defined to be the sum of the hashCodes of each entry in the map's entrySet view. This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any two maps t1 and t2, as required by the general contract of Object.hashCode.

Returns:
the hash code value for this map.

Reimplemented from java::util::AbstractMap.

virtual String java::util::TreeMap::toString (  )  const [virtual]

Returns a string representation of the object.

In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `</code>', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: <blockquote>

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 
</blockquote>

Returns:
a string representation of the object.

Reimplemented from java::util::AbstractMap.


The documentation for this class was generated from the following file:
Generated on Fri May 16 11:56:53 2008 for CrossPlatformJavaLikeC++API by  doxygen 1.5.3