Android
java.util
public class

java.util.TreeMap<K, V>

java.lang.Object
java.util.AbstractMap<K, V> Map<K, V>
java.util.TreeMap<K, V> Serializable Cloneable SortedMap<K, V>

TreeMap is an implementation of SortedMap. All optional operations are supported, adding and removing. The values can be any objects. The keys can be any objects which are comparable to each other either using their natural order or a specified Comparator.

Summary

Public Constructors

            TreeMap()
Constructs a new empty instance of TreeMap.
            TreeMap(Comparator<? super K> comparator)
Constructs a new empty instance of TreeMap which uses the specified Comparator.
            TreeMap(Map<? extends K, ? extends V> map)
Constructs a new instance of TreeMap containing the mappings from the specified Map and using the natural ordering.
            TreeMap(SortedMap<K, ? extends V> map)
Constructs a new instance of TreeMap containing the mappings from the specified SortedMap and using the same Comparator.

Public Methods

          void  clear()
Removes all mappings from this TreeMap, leaving it empty.
          Object  clone()
Returns a new TreeMap with the same mappings, size and comparator as this TreeMap.
          Comparator<? super K>  comparator()
Returns the Comparator used to compare elements in this TreeMap.
          boolean  containsKey(Object key)
Searches this TreeMap for the specified key.
          boolean  containsValue(Object value)
Searches this TreeMap for the specified value.
          Set<Entry<K, V>>  entrySet()
Returns a Set of the mappings contained in this TreeMap.
          firstKey()
Answer the first sorted key in this TreeMap.
          get(Object key)
Returns the value of the mapping with the specified key.
          SortedMap<K, V>  headMap(K endKey)
Returns a SortedMap of the specified portion of this TreeMap which contains keys less than the end key.
          Set<K>  keySet()
Returns a Set of the keys contained in this TreeMap.
          lastKey()
Answer the last sorted key in this TreeMap.
          put(K key, V value)
Maps the specified key to the specified value.
          void  putAll(Map<? extends K, ? extends V> map)
Copies every mapping in the specified Map to this TreeMap.
          remove(Object key)
Removes a mapping with the specified key from this TreeMap.
          int  size()
Returns the number of mappings in this TreeMap.
          SortedMap<K, V>  subMap(K startKey, K endKey)
Returns a SortedMap of the specified portion of this TreeMap which contains keys greater or equal to the start key but less than the end key.
          SortedMap<K, V>  tailMap(K startKey)
Returns a SortedMap of the specified portion of this TreeMap which contains keys greater or equal to the start key.
          Collection<V>  values()
Returns a Collection of the values contained in this TreeMap.
Methods inherited from class java.util.AbstractMap
Methods inherited from class java.lang.Object
Methods inherited from interface java.util.Map
Methods inherited from interface java.util.SortedMap

Details

Public Constructors

public TreeMap()

Constructs a new empty instance of TreeMap.

public TreeMap(Comparator<? super K> comparator)

Constructs a new empty instance of TreeMap which uses the specified Comparator.

Parameters

comparator the Comparator

public TreeMap(Map<? extends K, ? extends V> map)

Constructs a new instance of TreeMap containing the mappings from the specified Map and using the natural ordering.

Parameters

map the mappings to add

Throws

ClassCastException when a key in the Map does not implement the Comparable interface, or they keys in the Map cannot be compared

public TreeMap(SortedMap<K, ? extends V> map)

Constructs a new instance of TreeMap containing the mappings from the specified SortedMap and using the same Comparator.

Parameters

map the mappings to add

Public Methods

public void clear()

Removes all mappings from this TreeMap, leaving it empty.

See Also

public Object clone()

Returns a new TreeMap with the same mappings, size and comparator as this TreeMap.

Returns

  • a shallow copy of this TreeMap

See Also

public Comparator<? super K> comparator()

Returns the Comparator used to compare elements in this TreeMap.

Returns

  • a Comparator or null if the natural ordering is used

public boolean containsKey(Object key)

Searches this TreeMap for the specified key.

Parameters

key the object to search for

Returns

  • true if key is a key of this TreeMap, false otherwise

Throws

ClassCastException when the key cannot be compared with the keys in this TreeMap
NullPointerException when the key is null and the comparator cannot handle null

public boolean containsValue(Object value)

Searches this TreeMap for the specified value.

Parameters

value the object to search for

Returns

  • true if value is a value of this TreeMap, false otherwise

public Set<Entry<K, V>> entrySet()

Returns a Set of the mappings contained in this TreeMap. Each element in the set is a Map.Entry. The set is backed by this TreeMap so changes to one are reflected by the other. The set does not support adding.

Returns

  • a Set of the mappings

public K firstKey()

Answer the first sorted key in this TreeMap.

Returns

  • the first sorted key

Throws

NoSuchElementException when this TreeMap is empty

public V get(Object key)

Returns the value of the mapping with the specified key.

Parameters

key the key

Returns

  • the value of the mapping with the specified key

Throws

ClassCastException when the key cannot be compared with the keys in this TreeMap
NullPointerException when the key is null and the comparator cannot handle null

public SortedMap<K, V> headMap(K endKey)

Returns a SortedMap of the specified portion of this TreeMap which contains keys less than the end key. The returned SortedMap is backed by this TreeMap so changes to one are reflected by the other.

Parameters

endKey the end key

Returns

  • a sub-map where the keys are less than endKey

Throws

ClassCastException when the end key cannot be compared with the keys in this TreeMap
NullPointerException when the end key is null and the comparator cannot handle null

public Set<K> keySet()

Returns a Set of the keys contained in this TreeMap. The set is backed by this TreeMap so changes to one are reflected by the other. The set does not support adding.

Returns

  • a Set of the keys

public K lastKey()

Answer the last sorted key in this TreeMap.

Returns

  • the last sorted key

Throws

NoSuchElementException when this TreeMap is empty

public V put(K key, V value)

Maps the specified key to the specified value.

Parameters

key the key
value the value

Returns

  • the value of any previous mapping with the specified key or null if there was no mapping

Throws

ClassCastException when the key cannot be compared with the keys in this TreeMap
NullPointerException when the key is null and the comparator cannot handle null

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

Copies every mapping in the specified Map to this TreeMap.

Parameters

map the Map to copy mappings from

Throws

ClassCastException when a key in the Map cannot be compared with the keys in this TreeMap
NullPointerException when a key in the Map is null and the comparator cannot handle null

public V remove(Object key)

Removes a mapping with the specified key from this TreeMap.

Parameters

key the key of the mapping to remove

Returns

  • the value of the removed mapping or null if key is not a key in this TreeMap

Throws

ClassCastException when the key cannot be compared with the keys in this TreeMap
NullPointerException when the key is null and the comparator cannot handle null

public int size()

Returns the number of mappings in this TreeMap.

Returns

  • the number of mappings in this TreeMap

public SortedMap<K, V> subMap(K startKey, K endKey)

Returns a SortedMap of the specified portion of this TreeMap which contains keys greater or equal to the start key but less than the end key. The returned SortedMap is backed by this TreeMap so changes to one are reflected by the other.

Parameters

startKey the start key
endKey the end key

Returns

  • a sub-map where the keys are greater or equal to startKey and less than endKey

Throws

ClassCastException when the start or end key cannot be compared with the keys in this TreeMap
NullPointerException when the start or end key is null and the comparator cannot handle null

public SortedMap<K, V> tailMap(K startKey)

Returns a SortedMap of the specified portion of this TreeMap which contains keys greater or equal to the start key. The returned SortedMap is backed by this TreeMap so changes to one are reflected by the other.

Parameters

startKey the start key

Returns

  • a sub-map where the keys are greater or equal to startKey

Throws

ClassCastException when the start key cannot be compared with the keys in this TreeMap
NullPointerException when the start key is null and the comparator cannot handle null

public Collection<V> values()

Returns a Collection of the values contained in this TreeMap. The collection is backed by this TreeMap so changes to one are reflected by the other. The collection does not support adding.

Returns

  • a Collection of the values
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:56