Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
java.util.AbstractMap<K,V>
java.util.TreeMap<K,V>
This implementation is not synchronized. If you need to share this between
multiple threads, do something like:
SortedMap m
= Collections.synchronizedSortedMap(new TreeMap(...));
The iterators are fail-fast, meaning that any structural
modification, except for remove()
called on the iterator
itself, cause the iterator to throw a
ConcurrentModificationException
rather than exhibit
non-deterministic behavior.
Map
, HashMap
, Hashtable
, LinkedHashMap
, Comparable
, Comparator
, Collection
, Collections.synchronizedSortedMap(SortedMap)
, Serialized FormNested Class Summary |
Nested classes/interfaces inherited from class java.util.AbstractMap<K,V> | |
AbstractMap.SimpleEntry , AbstractMap.SimpleImmutableEntry |
Constructor Summary | |
| |
| |
| |
|
Method Summary | |
SortedMap |
|
NavigableMap |
|
SortedMap |
|
NavigableMap |
|
SortedMap |
|
NavigableMap |
|
Entry |
|
K |
|
void |
|
Object |
|
boolean |
|
boolean |
|
NavigableSet |
|
NavigableMap |
|
Set |
|
Entry |
|
K |
|
Entry |
|
K |
|
V | |
Entry |
|
K |
|
Set |
|
Entry |
|
K |
|
Entry |
|
K |
|
NavigableSet |
|
Entry |
|
Entry |
|
V |
|
void |
|
V | |
int |
|
Comparator |
|
Collection |
|
Methods inherited from class java.util.AbstractMap<K,V> | |
V>> entrySet , clear , clone , containsKey , containsValue , equals , get , hashCode , isEmpty , keySet , put , putAll , remove , size , toString , values |
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
public TreeMap()
Instantiate a new TreeMap with no elements, using the keys' natural ordering to sort. All entries in the map must have a key which implements Comparable, and which are mutually comparable, otherwise map operations may throw aClassCastException
. Attempts to use a null key will throw aNullPointerException
.
- See Also:
Comparable
public TreeMap(K> c)
Instantiate a new TreeMap with no elements, using the provided comparator to sort. All entries in the map must have keys which are mutually comparable by the Comparator, otherwise map operations may throw aClassCastException
.
- Parameters:
c
- the sort order for the keys of this map, or null for the natural order
public TreeMap(SortedMapsm)
Instantiate a new TreeMap, initializing it with all of the elements in the provided SortedMap. The elements will be sorted using the same comparator as in the provided SortedMap. This runs in linear time.
- Parameters:
sm
- a SortedMap, whose entries will be put into this TreeMap
- Throws:
NullPointerException
- if sm is null
public TreeMap(extends K, V> map)
Instantiate a new TreeMap, initializing it with all of the elements in the provided Map. The elements will be sorted using the natural ordering of the keys. This algorithm runs in n*log(n) time. All entries in the map must have keys which implement Comparable and are mutually comparable, otherwise map operations may throw aClassCastException
.
- Parameters:
map
- a Map, whose entries will be put into this TreeMap
- Throws:
ClassCastException
- if the keys in the provided Map are not comparableNullPointerException
- if map is null
- See Also:
Comparable
public SortedMapV> headMap (K toKey)
Returns a view of this Map including all entries with keys less thantoKey
. The returned map is backed by the original, so changes in one appear in the other. The submap will throw anIllegalArgumentException
for any attempt to access or add an element beyond the specified cutoff. The returned map does not include the endpoint; if you want inclusion, pass the successor element or callheadMap(toKey, true)
. This is equivalent to callingheadMap(toKey, false)
.
- Parameters:
toKey
- the (exclusive) cutoff point
- Returns:
- a view of the map less than the cutoff
- Throws:
ClassCastException
- iftoKey
is not compatible with the comparator (or is not Comparable, for natural ordering)NullPointerException
- if toKey is null, but the comparator does not tolerate null elements
public NavigableMapV> headMap (K toKey, boolean inclusive)
Returns a view of this Map including all entries with keys less than (or equal to, ifinclusive
is true)toKey
. The returned map is backed by the original, so changes in one appear in the other. The submap will throw anIllegalArgumentException
for any attempt to access or add an element beyond the specified cutoff.
- Parameters:
toKey
- the cutoff pointinclusive
- true if the cutoff point should be included.
- Returns:
- a view of the map less than (or equal to, if
inclusive
is true) the cutoff.
- Throws:
ClassCastException
- iftoKey
is not compatible with the comparator (or is not Comparable, for natural ordering)NullPointerException
- if toKey is null, but the comparator does not tolerate null elements
public SortedMapV> subMap (K fromKey, K toKey)
Returns a view of this Map including all entries with keys greater or equal tofromKey
and less thantoKey
(a half-open interval). The returned map is backed by the original, so changes in one appear in the other. The submap will throw anIllegalArgumentException
for any attempt to access or add an element beyond the specified cutoffs. The returned map includes the low endpoint but not the high; if you want to reverse this behavior on either end, pass in the successor element or callsubMap(K,boolean,K,boolean)
. This call is equivalent tosubMap(fromKey, true, toKey, false)
.
- Parameters:
fromKey
- the (inclusive) low cutoff pointtoKey
- the (exclusive) high cutoff point
- Returns:
- a view of the map between the cutoffs
- Throws:
ClassCastException
- if either cutoff is not compatible with the comparator (or is not Comparable, for natural ordering)NullPointerException
- if fromKey or toKey is null, but the comparator does not tolerate null elementsIllegalArgumentException
- if fromKey is greater than toKey
public NavigableMapV> subMap (K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
Returns a view of this Map including all entries with keys greater (or equal to, iffromInclusive
is true)fromKey
and less than (or equal to, iftoInclusive
is true)toKey
. The returned map is backed by the original, so changes in one appear in the other. The submap will throw anIllegalArgumentException
for any attempt to access or add an element beyond the specified cutoffs.
- Parameters:
fromKey
- the low cutoff pointfromInclusive
- true if the low cutoff point should be included.toKey
- the high cutoff pointtoInclusive
- true if the high cutoff point should be included.
- Returns:
- a view of the map for the specified range.
- Throws:
ClassCastException
- if either cutoff is not compatible with the comparator (or is not Comparable, for natural ordering)NullPointerException
- if fromKey or toKey is null, but the comparator does not tolerate null elementsIllegalArgumentException
- if fromKey is greater than toKey
public SortedMapV> tailMap (K fromKey)
Returns a view of this Map including all entries with keys greater or equal tofromKey
. The returned map is backed by the original, so changes in one appear in the other. The submap will throw anIllegalArgumentException
for any attempt to access or add an element beyond the specified cutoff. The returned map includes the endpoint; if you want to exclude it, pass in the successor element. This is equivalent to callingtailMap(fromKey, true)
.
- Parameters:
fromKey
- the (inclusive) low cutoff point
- Returns:
- a view of the map above the cutoff
- Throws:
ClassCastException
- iffromKey
is not compatible with the comparator (or is not Comparable, for natural ordering)NullPointerException
- if fromKey is null, but the comparator does not tolerate null elements
public NavigableMapV> tailMap (K fromKey, boolean inclusive)
Returns a view of this Map including all entries with keys greater or equal tofromKey
. The returned map is backed by the original, so changes in one appear in the other. The submap will throw anIllegalArgumentException
for any attempt to access or add an element beyond the specified cutoff. The returned map includes the endpoint; if you want to exclude it, pass in the successor element.
- Parameters:
fromKey
- the low cutoff pointinclusive
- true if the cutoff point should be included.
- Returns:
- a view of the map above the cutoff
- Throws:
ClassCastException
- iffromKey
is not compatible with the comparator (or is not Comparable, for natural ordering)NullPointerException
- if fromKey is null, but the comparator does not tolerate null elements
public EntryceilingEntry(K key)
Returns the entry associated with the least or lowest key that is greater than or equal to the specified key, ornull
if there is no such key.
- Parameters:
key
- the key relative to the returned entry.
- Returns:
- the entry with the least key greater than or equal to the given key, or
null
if there is no such key.
- Throws:
ClassCastException
- if the specified key can not be compared with those in the map.NullPointerException
- if the key isnull
and this map either uses natural ordering or a comparator that does not permit null keys.
- Since:
- 1.6
public K ceilingKey(K key)
Returns the the least or lowest key that is greater than or equal to the specified key, ornull
if there is no such key.
- Parameters:
key
- the key relative to the returned entry.
- Returns:
- the least key greater than or equal to the given key, or
null
if there is no such key.
- Throws:
ClassCastException
- if the specified key can not be compared with those in the map.NullPointerException
- if the key isnull
and this map either uses natural ordering or a comparator that does not permit null keys.
- Since:
- 1.6
public void clear()
Clears the Map so it has no keys. This is O(1).
- Overrides:
- clear in interface AbstractMap<K,V>
public Object clone()
Returns a shallow clone of this TreeMap. The Map itself is cloned, but its contents are not.
- Overrides:
- clone in interface AbstractMap<K,V>
- Returns:
- the clone
public boolean containsKey(Object key)
Returns true if the map contains a mapping for the given key.
- Specified by:
- containsKey in interface Map<K,V>
- Overrides:
- containsKey in interface AbstractMap<K,V>
- Parameters:
key
- the key to look for
- Returns:
- true if the key has a mapping
- Throws:
ClassCastException
- if key is not comparable to map elementsNullPointerException
- if key is null and the comparator is not tolerant of nulls
public boolean containsValue(Object value)
Returns true if the map contains at least one mapping to the given value. This requires linear time.
- Specified by:
- containsValue in interface Map<K,V>
- Overrides:
- containsValue in interface AbstractMap<K,V>
- Parameters:
value
- the value to look for
- Returns:
- true if the value appears in a mapping
public NavigableSetdescendingKeySet()
Returns a reverse orderedNavigableSet
view of this map's keys. The set is backed by theTreeMap
, so changes in one show up in the other. The set supports element removal, but not element addition.
- Returns:
- a reverse ordered set view of the keys.
- Since:
- 1.6
- See Also:
descendingMap()
public NavigableMapdescendingMap()
Returns a view of the map in reverse order. The descending map is backed by the original map, so that changes affect both maps. Any changes occurring to either map while an iteration is taking place (with the exception of aIterator.remove()
operation) result in undefined behaviour from the iteration. The ordering of the descending map is the same as for a map with aComparator
given byCollections.reverseOrder()
, and callingdescendingMap()
on the descending map itself results in a view equivalent to the original map.
- Returns:
- a reverse order view of the map.
- Since:
- 1.6
public Set> entrySet()
Returns a "set view" of this TreeMap's entries. The set is backed by the TreeMap, so changes in one show up in the other. The set supports element removal, but not element addition.Note that the iterators for all three views, from keySet(), entrySet(), and values(), traverse the TreeMap in sorted sequence.
- Returns:
- a set view of the entries
public EntryfirstEntry()
Returns the entry associated with the least or lowest key in the map, ornull
if the map is empty.
- Returns:
- the lowest entry, or
null
if the map is empty.
- Since:
- 1.6
public K firstKey()
Returns the first (lowest) key in the map.
- Returns:
- the first key
- Throws:
NoSuchElementException
- if the map is empty
public EntryfloorEntry(K key)
Returns the entry associated with the greatest or highest key that is less than or equal to the specified key, ornull
if there is no such key.
- Parameters:
key
- the key relative to the returned entry.
- Returns:
- the entry with the greatest key less than or equal to the given key, or
null
if there is no such key.
- Throws:
ClassCastException
- if the specified key can not be compared with those in the map.NullPointerException
- if the key isnull
and this map either uses natural ordering or a comparator that does not permit null keys.
- Since:
- 1.6
public K floorKey(K key)
Returns the the greatest or highest key that is less than or equal to the specified key, ornull
if there is no such key.
- Parameters:
key
- the key relative to the returned entry.
- Returns:
- the greatest key less than or equal to the given key, or
null
if there is no such key.
- Throws:
ClassCastException
- if the specified key can not be compared with those in the map.NullPointerException
- if the key isnull
and this map either uses natural ordering or a comparator that does not permit null keys.
- Since:
- 1.6
public V get(Object key)
Return the value in this TreeMap associated with the supplied key, ornull
if the key maps to nothing. NOTE: Since the value could also be null, you must use containsKey to see if this key actually maps to something.
- Overrides:
- get in interface AbstractMap<K,V>
- Parameters:
key
- the key for which to fetch an associated value
- Returns:
- what the key maps to, if present
- Throws:
ClassCastException
- if key is not comparable to elements in the mapNullPointerException
- if key is null but the comparator does not tolerate nulls
- See Also:
put(Object, Object)
,containsKey(Object)
public EntryhigherEntry(K key)
Returns the entry associated with the least or lowest key that is strictly greater than the specified key, ornull
if there is no such key.
- Parameters:
key
- the key relative to the returned entry.
- Returns:
- the entry with the least key greater than the given key, or
null
if there is no such key.
- Throws:
ClassCastException
- if the specified key can not be compared with those in the map.NullPointerException
- if the key isnull
and this map either uses natural ordering or a comparator that does not permit null keys.
- Since:
- 1.6
public K higherKey(K key)
Returns the the least or lowest key that is strictly greater than the specified key, ornull
if there is no such key.
- Parameters:
key
- the key relative to the returned entry.
- Returns:
- the least key greater than the given key, or
null
if there is no such key.
- Throws:
ClassCastException
- if the specified key can not be compared with those in the map.NullPointerException
- if the key isnull
and this map either uses natural ordering or a comparator that does not permit null keys.
- Since:
- 1.6
public SetkeySet()
Returns a "set view" of this TreeMap's keys. The set is backed by the TreeMap, so changes in one show up in the other. The set supports element removal, but not element addition.
- Overrides:
- keySet in interface AbstractMap<K,V>
- Returns:
- a set view of the keys
- See Also:
values()
,entrySet()
public EntrylastEntry()
Returns the entry associated with the greatest or highest key in the map, ornull
if the map is empty.
- Returns:
- the highest entry, or
null
if the map is empty.
- Since:
- 1.6
public K lastKey()
Returns the last (highest) key in the map.
- Returns:
- the last key
- Throws:
NoSuchElementException
- if the map is empty
public EntrylowerEntry(K key)
Returns the entry associated with the greatest or highest key that is strictly less than the specified key, ornull
if there is no such key.
- Parameters:
key
- the key relative to the returned entry.
- Returns:
- the entry with the greatest key less than the given key, or
null
if there is no such key.
- Throws:
ClassCastException
- if the specified key can not be compared with those in the map.NullPointerException
- if the key isnull
and this map either uses natural ordering or a comparator that does not permit null keys.
- Since:
- 1.6
public K lowerKey(K key)
Returns the the greatest or highest key that is strictly less than the specified key, ornull
if there is no such key.
- Parameters:
key
- the key relative to the returned entry.
- Returns:
- the greatest key less than the given key, or
null
if there is no such key.
- Throws:
ClassCastException
- if the specified key can not be compared with those in the map.NullPointerException
- if the key isnull
and this map either uses natural ordering or a comparator that does not permit null keys.
- Since:
- 1.6
public NavigableSetnavigableKeySet()
Returns aNavigableSet
view of this map's keys. The set is backed by theTreeMap
, so changes in one show up in the other. Any changes occurring to either while an iteration is taking place (with the exception of aIterator.remove()
operation) result in undefined behaviour from the iteration. The ordering The set supports element removal, but not element addition.
- Returns:
- a
NavigableSet
view of the keys.
- Since:
- 1.6
public EntrypollFirstEntry()
Removes and returns the entry associated with the least or lowest key in the map, ornull
if the map is empty.
- Returns:
- the removed first entry, or
null
if the map is empty.
- Since:
- 1.6
public EntrypollLastEntry()
Removes and returns the entry associated with the greatest or highest key in the map, ornull
if the map is empty.
- Returns:
- the removed last entry, or
null
if the map is empty.
- Since:
- 1.6
public V put(K key, V value)
Puts the supplied value into the Map, mapped by the supplied key. The value may be retrieved by any object whichequals()
this key. NOTE: Since the prior value could also be null, you must first use containsKey if you want to see if you are replacing the key's mapping.
- Overrides:
- put in interface AbstractMap<K,V>
- Parameters:
key
- the key used to locate the valuevalue
- the value to be stored in the Map
- Returns:
- the prior mapping of the key, or null if there was none
- Throws:
ClassCastException
- if key is not comparable to current map keysNullPointerException
- if key is null, but the comparator does not tolerate nulls
- See Also:
get(Object)
,Object.equals(Object)
public void putAll(extends K, V> m)
Copies all elements of the given map into this TreeMap. If this map already has a mapping for a key, the new mapping replaces the current one.
- Overrides:
- putAll in interface AbstractMap<K,V>
- Parameters:
m
- the map to be added
- Throws:
ClassCastException
- if a key in m is not comparable with keys in the mapNullPointerException
- if a key in m is null, and the comparator does not tolerate nulls
public V remove(Object key)
Removes from the TreeMap and returns the value which is mapped by the supplied key. If the key maps to nothing, then the TreeMap remains unchanged, andnull
is returned. NOTE: Since the value could also be null, you must use containsKey to see if you are actually removing a mapping.
- Overrides:
- remove in interface AbstractMap<K,V>
- Parameters:
key
- the key used to locate the value to remove
- Returns:
- whatever the key mapped to, if present
- Throws:
ClassCastException
- if key is not comparable to current map keysNullPointerException
- if key is null, but the comparator does not tolerate nulls
public int size()
Returns the number of key-value mappings currently in this Map.
- Overrides:
- size in interface AbstractMap<K,V>
- Returns:
- the size
public Comparatorsuper K> comparator()
Return the comparator used to sort this map, or null if it is by natural order.
- Returns:
- the map's comparator
public Collectionvalues()
Returns a "collection view" (or "bag view") of this TreeMap's values. The collection is backed by the TreeMap, so changes in one show up in the other. The collection supports element removal, but not element addition.
- Overrides:
- values in interface AbstractMap<K,V>
- Returns:
- a bag view of the values
- See Also:
keySet()
,entrySet()