java.util
Class TreeMap<K,V>

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
      extended by java.util.TreeMap<K,V>
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Map sample code for java.util.Map definition code for java.util.Map <K,V>, SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>

public class TreeMap<K,V>
extends AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
implements SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>, Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Serializable sample code for java.io.Serializable definition code for java.io.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.

Since:
1.2
See Also:
Map sample code for java.util.Map definition code for java.util.Map , HashMap sample code for java.util.HashMap definition code for java.util.HashMap , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable , Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable , Comparator sample code for java.util.Comparator definition code for java.util.Comparator , Collection sample code for java.util.Collection definition code for java.util.Collection , Collections.synchronizedMap(Map) sample code for java.util.Collections.synchronizedMap(java.util.Map) definition code for java.util.Collections.synchronizedMap(java.util.Map) , Serialized Form

Constructor Summary
TreeMap sample code for java.util.TreeMap.TreeMap() definition code for java.util.TreeMap.TreeMap() ()
          Constructs a new, empty map, sorted according to the keys' natural order.
TreeMap sample code for java.util.TreeMap.TreeMap(java.util.Comparator) definition code for java.util.TreeMap.TreeMap(java.util.Comparator) (Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super K> c)
          Constructs a new, empty map, sorted according to the given comparator.
TreeMap sample code for java.util.TreeMap.TreeMap(java.util.Map) definition code for java.util.TreeMap.TreeMap(java.util.Map) (Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> m)
          Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order.
TreeMap sample code for java.util.TreeMap.TreeMap(java.util.SortedMap) definition code for java.util.TreeMap.TreeMap(java.util.SortedMap) (SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,? extends V> m)
          Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering.
 
Method Summary
 void clear sample code for java.util.TreeMap.clear() definition code for java.util.TreeMap.clear() ()
          Removes all mappings from this TreeMap.
 Object sample code for java.lang.Object definition code for java.lang.Object clone sample code for java.util.TreeMap.clone() definition code for java.util.TreeMap.clone() ()
          Returns a shallow copy of this TreeMap instance.
 Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super K> comparator sample code for java.util.TreeMap.comparator() definition code for java.util.TreeMap.comparator() ()
          Returns the comparator used to order this map, or null if this map uses its keys' natural order.
 boolean containsKey sample code for java.util.TreeMap.containsKey(java.lang.Object) definition code for java.util.TreeMap.containsKey(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue sample code for java.util.TreeMap.containsValue(java.lang.Object) definition code for java.util.TreeMap.containsValue(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  value)
          Returns true if this map maps one or more keys to the specified value.
 Set sample code for java.util.Set definition code for java.util.Set <Map.Entry sample code for java.util.Map.Entry definition code for java.util.Map.Entry <K,V>> entrySet sample code for java.util.TreeMap.entrySet() definition code for java.util.TreeMap.entrySet() ()
          Returns a set view of the mappings contained in this map.
 K firstKey sample code for java.util.TreeMap.firstKey() definition code for java.util.TreeMap.firstKey() ()
          Returns the first (lowest) key currently in this sorted map.
 V get sample code for java.util.TreeMap.get(java.lang.Object) definition code for java.util.TreeMap.get(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  key)
          Returns the value to which this map maps the specified key.
 SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V> headMap sample code for java.util.TreeMap.headMap(K) definition code for java.util.TreeMap.headMap(K) (K toKey)
          Returns a view of the portion of this map whose keys are strictly less than toKey.
 Set sample code for java.util.Set definition code for java.util.Set <K> keySet sample code for java.util.TreeMap.keySet() definition code for java.util.TreeMap.keySet() ()
          Returns a Set view of the keys contained in this map.
 K lastKey sample code for java.util.TreeMap.lastKey() definition code for java.util.TreeMap.lastKey() ()
          Returns the last (highest) key currently in this sorted map.
 V put sample code for java.util.TreeMap.put(K, V) definition code for java.util.TreeMap.put(K, V) (K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll sample code for java.util.TreeMap.putAll(java.util.Map) definition code for java.util.TreeMap.putAll(java.util.Map) (Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> map)
          Copies all of the mappings from the specified map to this map.
 V remove sample code for java.util.TreeMap.remove(java.lang.Object) definition code for java.util.TreeMap.remove(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  key)
          Removes the mapping for this key from this TreeMap if present.
 int size sample code for java.util.TreeMap.size() definition code for java.util.TreeMap.size() ()
          Returns the number of key-value mappings in this map.
 SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V> subMap sample code for java.util.TreeMap.subMap(K, K) definition code for java.util.TreeMap.subMap(K, K) (K fromKey, K toKey)
          Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
 SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V> tailMap sample code for java.util.TreeMap.tailMap(K) definition code for java.util.TreeMap.tailMap(K) (K fromKey)
          Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
 Collection sample code for java.util.Collection definition code for java.util.Collection <V> values sample code for java.util.TreeMap.values() definition code for java.util.TreeMap.values() ()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class java.util.AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap
equals sample code for java.util.AbstractMap.equals(java.lang.Object) definition code for java.util.AbstractMap.equals(java.lang.Object) , hashCode sample code for java.util.AbstractMap.hashCode() definition code for java.util.AbstractMap.hashCode() , isEmpty sample code for java.util.AbstractMap.isEmpty() definition code for java.util.AbstractMap.isEmpty() , toString sample code for java.util.AbstractMap.toString() definition code for java.util.AbstractMap.toString()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.util.Map sample code for java.util.Map definition code for java.util.Map
equals sample code for java.util.Map.equals(java.lang.Object) definition code for java.util.Map.equals(java.lang.Object) , hashCode sample code for java.util.Map.hashCode() definition code for java.util.Map.hashCode() , isEmpty sample code for java.util.Map.isEmpty() definition code for java.util.Map.isEmpty()
 

Constructor Detail

TreeMap sample code for java.util.TreeMap() definition code for java.util.TreeMap()

public TreeMap()
Constructs a new, empty map, sorted according to the keys' natural order. All keys inserted into the 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. If the user attempts to put a key into the map that violates this constraint (for example, the user attempts to put a string key into a map whose keys are integers), the put(Object key, Object value) call will throw a ClassCastException.

See Also:
Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable

TreeMap sample code for java.util.TreeMap(java.util.Comparator<? super K>) definition code for java.util.TreeMap(java.util.Comparator<? super K>)

public TreeMap(Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super K> c)
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.

TreeMap sample code for java.util.TreeMap(java.util.Map<? extends K, ? extends V>) definition code for java.util.TreeMap(java.util.Map<? extends K, ? extends V>)

public TreeMap(Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> 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.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - the keys in t are not Comparable, or are not mutually comparable.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified map is null.

TreeMap sample code for java.util.TreeMap(java.util.SortedMap<K, ? extends V>) definition code for java.util.TreeMap(java.util.SortedMap<K, ? extends V>)

public TreeMap(SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,? extends V> 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.
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified sorted map is null.
Method Detail

size sample code for java.util.TreeMap.size() definition code for java.util.TreeMap.size()

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

Specified by:
size sample code for java.util.Map.size() definition code for java.util.Map.size() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
size sample code for java.util.AbstractMap.size() definition code for java.util.AbstractMap.size() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
the number of key-value mappings in this map.

containsKey sample code for java.util.TreeMap.containsKey(java.lang.Object) definition code for java.util.TreeMap.containsKey(java.lang.Object)

public boolean containsKey(Object sample code for java.lang.Object definition code for java.lang.Object  key)
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey sample code for java.util.Map.containsKey(java.lang.Object) definition code for java.util.Map.containsKey(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
containsKey sample code for java.util.AbstractMap.containsKey(java.lang.Object) definition code for java.util.AbstractMap.containsKey(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Parameters:
key - key whose presence in this map is to be tested.
Returns:
true if this map contains a mapping for the specified key.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - if the key cannot be compared with the keys currently in the map.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - key is null and this map uses natural ordering, or its comparator does not tolerate null keys.

containsValue sample code for java.util.TreeMap.containsValue(java.lang.Object) definition code for java.util.TreeMap.containsValue(java.lang.Object)

public boolean containsValue(Object sample code for java.lang.Object definition code for java.lang.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.

Specified by:
containsValue sample code for java.util.Map.containsValue(java.lang.Object) definition code for java.util.Map.containsValue(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
containsValue sample code for java.util.AbstractMap.containsValue(java.lang.Object) definition code for java.util.AbstractMap.containsValue(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
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

get sample code for java.util.TreeMap.get(java.lang.Object) definition code for java.util.TreeMap.get(java.lang.Object)

public V get(Object sample code for java.lang.Object definition code for java.lang.Object  key)
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.

Specified by:
get sample code for java.util.Map.get(java.lang.Object) definition code for java.util.Map.get(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
get sample code for java.util.AbstractMap.get(java.lang.Object) definition code for java.util.AbstractMap.get(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
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 the key.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - key cannot be compared with the keys currently in the map.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - key is null and this map uses natural ordering, or its comparator does not tolerate null keys.
See Also:
containsKey(Object) sample code for java.util.TreeMap.containsKey(java.lang.Object) definition code for java.util.TreeMap.containsKey(java.lang.Object)

comparator sample code for java.util.TreeMap.comparator() definition code for java.util.TreeMap.comparator()

public Comparator sample code for java.util.Comparator definition code for java.util.Comparator <? super K> comparator()
Returns the comparator used to order this map, or null if this map uses its keys' natural order.

Specified by:
comparator sample code for java.util.SortedMap.comparator() definition code for java.util.SortedMap.comparator() in interface SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>
Returns:
the comparator associated with this sorted map, or null if it uses its keys' natural sort method.

firstKey sample code for java.util.TreeMap.firstKey() definition code for java.util.TreeMap.firstKey()

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

Specified by:
firstKey sample code for java.util.SortedMap.firstKey() definition code for java.util.SortedMap.firstKey() in interface SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>
Returns:
the first (lowest) key currently in this sorted map.
Throws:
NoSuchElementException sample code for java.util.NoSuchElementException definition code for java.util.NoSuchElementException - Map is empty.

lastKey sample code for java.util.TreeMap.lastKey() definition code for java.util.TreeMap.lastKey()

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

Specified by:
lastKey sample code for java.util.SortedMap.lastKey() definition code for java.util.SortedMap.lastKey() in interface SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>
Returns:
the last (highest) key currently in this sorted map.
Throws:
NoSuchElementException sample code for java.util.NoSuchElementException definition code for java.util.NoSuchElementException - Map is empty.

putAll sample code for java.util.TreeMap.putAll(java.util.Map<? extends K, ? extends V>) definition code for java.util.TreeMap.putAll(java.util.Map<? extends K, ? extends V>)

public void putAll(Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map. These mappings replace any mappings that this map had for any of the keys currently in the specified map.

Specified by:
putAll sample code for java.util.Map.putAll(java.util.Map) definition code for java.util.Map.putAll(java.util.Map) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
putAll sample code for java.util.AbstractMap.putAll(java.util.Map) definition code for java.util.AbstractMap.putAll(java.util.Map) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Parameters:
map - mappings to be stored in this map.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - class of a key or value in the specified map prevents it from being stored in this map.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the given map is null or this map does not permit null keys and a key in the specified map is null.

put sample code for java.util.TreeMap.put(K, V) definition code for java.util.TreeMap.put(K, V)

public V put(K key,
             V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

Specified by:
put sample code for java.util.Map.put(K, V) definition code for java.util.Map.put(K, V) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
put sample code for java.util.AbstractMap.put(K, V) definition code for java.util.AbstractMap.put(K, V) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
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.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - key cannot be compared with the keys currently in the map.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - key is null and this map uses natural order, or its comparator does not tolerate null keys.

remove sample code for java.util.TreeMap.remove(java.lang.Object) definition code for java.util.TreeMap.remove(java.lang.Object)

public V remove(Object sample code for java.lang.Object definition code for java.lang.Object  key)
Removes the mapping for this key from this TreeMap if present.

Specified by:
remove sample code for java.util.Map.remove(java.lang.Object) definition code for java.util.Map.remove(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
remove sample code for java.util.AbstractMap.remove(java.lang.Object) definition code for java.util.AbstractMap.remove(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Parameters:
key - key for which mapping should be removed
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.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - key cannot be compared with the keys currently in the map.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - key is null and this map uses natural order, or its comparator does not tolerate null keys.

clear sample code for java.util.TreeMap.clear() definition code for java.util.TreeMap.clear()

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

Specified by:
clear sample code for java.util.Map.clear() definition code for java.util.Map.clear() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
clear sample code for java.util.AbstractMap.clear() definition code for java.util.AbstractMap.clear() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>

clone sample code for java.util.TreeMap.clone() definition code for java.util.TreeMap.clone()

public Object sample code for java.lang.Object definition code for java.lang.Object  clone()
Returns a shallow copy of this TreeMap instance. (The keys and values themselves are not cloned.)

Overrides:
clone sample code for java.util.AbstractMap.clone() definition code for java.util.AbstractMap.clone() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
a shallow copy of this Map.
See Also:
Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable

keySet sample code for java.util.TreeMap.keySet() definition code for java.util.TreeMap.keySet()

public Set sample code for java.util.Set definition code for java.util.Set <K> keySet()
Returns a Set view of the keys contained in this map. The set's iterator will return the keys in ascending order. The map is backed by this TreeMap instance, so changes to this map are reflected in the Set, and vice-versa. 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.

Specified by:
keySet sample code for java.util.Map.keySet() definition code for java.util.Map.keySet() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
keySet sample code for java.util.AbstractMap.keySet() definition code for java.util.AbstractMap.keySet() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
a set view of the keys contained in this TreeMap.

values sample code for java.util.TreeMap.values() definition code for java.util.TreeMap.values()

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

Specified by:
values sample code for java.util.Map.values() definition code for java.util.Map.values() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Overrides:
values sample code for java.util.AbstractMap.values() definition code for java.util.AbstractMap.values() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
a collection view of the values contained in this map.

entrySet sample code for java.util.TreeMap.entrySet() definition code for java.util.TreeMap.entrySet()

public Set sample code for java.util.Set definition code for java.util.Set <Map.Entry sample code for java.util.Map.Entry definition code for java.util.Map.Entry <K,V>> entrySet()
Returns a set view of the mappings contained in this map. The set's iterator returns the mappings in ascending key order. Each element in the returned set is a Map.Entry. The set is backed by this map, so changes to this map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from the TreeMap, through the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet sample code for java.util.Map.entrySet() definition code for java.util.Map.entrySet() in interface Map sample code for java.util.Map definition code for java.util.Map <K,V>
Specified by:
entrySet sample code for java.util.AbstractMap.entrySet() definition code for java.util.AbstractMap.entrySet() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
Returns:
a set view of the mappings contained in this map.
See Also:
Map.Entry sample code for java.util.Map.Entry definition code for java.util.Map.Entry

subMap sample code for java.util.TreeMap.subMap(K, K) definition code for java.util.TreeMap.subMap(K, K)

public SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V> subMap(K fromKey,
                             K toKey)
Returns a view of the portion of this 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 map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

The sorted 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 sorted 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:

    SortedMap sub = m.submap(low, high+"\0");
A similar 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:
    SortedMap sub = m.subMap(low+"\0", high);

Specified by:
subMap sample code for java.util.SortedMap.subMap(K, K) definition code for java.util.SortedMap.subMap(K, K) in interface SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>
Parameters:
fromKey - low endpoint (inclusive) of the subMap.
toKey - high endpoint (exclusive) of the subMap.
Returns:
a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.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).
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if fromKey is greater than toKey.
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if fromKey or toKey is null and this map uses natural order, or its comparator does not tolerate null keys.

headMap sample code for java.util.TreeMap.headMap(K) definition code for java.util.TreeMap.headMap(K)

public SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V> headMap(K toKey)
Returns a view of the portion of this map whose keys are strictly less than toKey. The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

The sorted 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 sorted 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:

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

Specified by:
headMap sample code for java.util.SortedMap.headMap(K) definition code for java.util.SortedMap.headMap(K) in interface SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>
Parameters:
toKey - high endpoint (exclusive) of the headMap.
Returns:
a view of the portion of this map whose keys are strictly less than toKey.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - if toKey is not compatible with this map's comparator (or, if the map has no comparator, if toKey does not implement Comparable).
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.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 sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if toKey is null and this map uses natural order, or its comparator does not tolerate null keys.

tailMap sample code for java.util.TreeMap.tailMap(K) definition code for java.util.TreeMap.tailMap(K)

public SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V> tailMap(K fromKey)
Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key less than fromKey.

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 m is a sorted 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:

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

Specified by:
tailMap sample code for java.util.SortedMap.tailMap(K) definition code for java.util.SortedMap.tailMap(K) in interface SortedMap sample code for java.util.SortedMap definition code for java.util.SortedMap <K,V>
Parameters:
fromKey - low endpoint (inclusive) of the tailMap.
Returns:
a view of the portion of this map whose keys are greater than or equal to fromKey.
Throws:
ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.ClassCastException - if fromKey is not compatible with this map's comparator (or, if the map has no comparator, if fromKey does not implement Comparable).
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.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 sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if fromKey is null and this map uses natural order, or its comparator does not tolerate null keys.