Class TreeMap<K,V>
- Type Parameters:
K- type of keyV- type of value
- All Implemented Interfaces:
Map<K,V>, NavigableMap<K, V>, SortedMap<K, V>
- Since:
- 1.2
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorsConstructorDescriptionTreeMap()Constructs a new emptyTreeMapinstance.TreeMap(Comparator<? super K> comparator) Constructs a new emptyTreeMapinstance with the specified comparator.Constructs a newTreeMapinstance containing the mappings from the specified map and using natural ordering.Constructs a newTreeMapinstance containing the mappings from the specified SortedMap and using the same comparator. -
Method Summary
Modifier and TypeMethodDescriptionceilingEntry(K key) Answers an entry related with the smallest key greater than or equal to the specified key, or null if no such key.ceilingKey(K key) Answers the smallest key greater than or equal to the specified key, or null if no such key.voidclear()Removes all mappings from this TreeMap, leaving it empty.Comparator<? super K> Returns the comparator used to compare elements in this map.booleancontainsKey(Object key) Returns whether this map contains the specified key.booleancontainsValue(Object value) Returns whether this map contains the specified value.Answers a NavigableSet view of the keys in descending order.Answers a reverse order view of the map.entrySet()Returns a set containing all of the mappings in this map.Answers the entry with the smallest key, or null if the map is empty.firstKey()Returns the first key in this map.floorEntry(K key) Answers an entry related with the biggest key less than or equal to the specified key, or null if no such key.Answers the biggest key less than or equal to the specified key, or null if no such key.Returns the value of the mapping with the specified key.Returns a sorted map over a range of this sorted map with all keys that are less than the specifiedendKey.Answers a view of the head of the map whose keys are smaller than (or equal to, depends on inclusive argument) endKey.higherEntry(K key) Answers an entry related with the smallest key greater than the specified key, or null if no such key.Answers the smallest key greater than the specified key, or null if no such key.keySet()Returns a set of the keys contained in this map.Answers the entry with the biggest key, or null if the map is empty.lastKey()Returns the last key in this map.lowerEntry(K key) Answers an entry related with the biggest key less than the specified key, or null if no such key.Answers the biggest key less than the specified key, or null if no such key.Answers a NavigableSet view of the keys in ascending order.Deletes and answers the entry with the smallest key, or null if the map is empty.Deletes and answers the entry with the biggest key, or null if the map is empty.Maps the specified key to the specified value.voidCopies all the mappings in the given map to this map.Removes the mapping with the specified key from this map.intsize()Returns the number of mappings in this map.Answers a view of part of the map whose keys is from startKey to endKey.Returns a sorted map over a range of this sorted map with all keys greater than or equal to the specifiedstartKeyand less than the specifiedendKey.Returns a sorted map over a range of this sorted map with all keys that are greater than or equal to the specifiedstartKey.Answers a view of the tail of the map whose keys are bigger than (or equal to, depends on inclusive argument) startKey.values()Returns a collection of the values contained in this map.Methods inherited from class AbstractMap
equals, hashCode, isEmpty, toString
-
Constructor Details
-
TreeMap
public TreeMap()Constructs a new emptyTreeMapinstance. -
TreeMap
Constructs a new emptyTreeMapinstance with the specified comparator.- Parameters:
comparator- the comparator to compare keys with.
-
TreeMap
Constructs a newTreeMapinstance containing the mappings from the specified map and using natural ordering.- Parameters:
map- the mappings to add.- Throws:
ClassCastException- if a key in the specified map does not implement the Comparable interface, or if the keys in the map cannot be compared.
-
TreeMap
-
-
Method Details
-
clear
-
comparator
Returns the comparator used to compare elements in this map.- Specified by:
comparatorin interfaceSortedMap<K,V> - Returns:
- the comparator or
nullif the natural ordering is used.
-
containsKey
Returns whether this map contains the specified key.- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V> - Parameters:
key- the key to search for.- Returns:
trueif this map contains the specified key,falseotherwise.- Throws:
ClassCastException- if the specified key cannot be compared with the keys in this map.NullPointerException- if the specified key isnulland the comparator cannot handlenullkeys.
-
containsValue
Returns whether this map contains the specified value.- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V> - Parameters:
value- the value to search for.- Returns:
trueif this map contains the specified value,falseotherwise.
-
firstKey
-
get
Returns the value of the mapping with the specified key.- Specified by:
getin interfaceMap<K,V> - Overrides:
getin classAbstractMap<K,V> - Parameters:
key- the key.- Returns:
- the value of the mapping with the specified key.
- Throws:
ClassCastException- if the key cannot be compared with the keys in this map.NullPointerException- if the key isnulland the comparator cannot handlenull.
-
keySet
-
lastKey
-
put
Maps the specified key to the specified value.- Specified by:
putin interfaceMap<K,V> - Overrides:
putin classAbstractMap<K,V> - Parameters:
key- the key.value- the value.- Returns:
- the value of any previous mapping with the specified key or
nullif there was no mapping. - Throws:
ClassCastException- if the specified key cannot be compared with the keys in this map.NullPointerException- if the specified key isnulland the comparator cannot handlenullkeys.
-
putAll
Copies all the mappings in the given map to this map. These mappings will replace all mappings that this map had for any of the keys currently in the given map.- Specified by:
putAllin interfaceMap<K,V> - Overrides:
putAllin classAbstractMap<K,V> - Parameters:
map- the map to copy mappings from.- Throws:
ClassCastException- if a key in the specified map cannot be compared with the keys in this map.NullPointerException- if a key in the specified map isnulland the comparator cannot handlenullkeys.
-
remove
Removes the mapping with the specified key from this map.- Specified by:
removein interfaceMap<K,V> - Overrides:
removein classAbstractMap<K,V> - Parameters:
key- the key of the mapping to remove.- Returns:
- the value of the removed mapping or
nullif no mapping for the specified key was found. - Throws:
ClassCastException- if the specified key cannot be compared with the keys in this map.NullPointerException- if the specified key isnulland the comparator cannot handlenullkeys.
-
size
-
values
Returns a collection of the values contained in this map. The collection is backed by this map so changes to one are reflected by the other. The collection supports remove, removeAll, retainAll and clear operations, and it does not support add or addAll operations.This method returns a collection which is the subclass of AbstractCollection. The iterator method of this subclass returns a "wrapper object" over the iterator of map's entrySet(). The
sizemethod wraps the map's size method and thecontainsmethod wraps the map's containsValue method.The collection is created when this method is called for the first time and returned in response to all subsequent calls. This method may return different collections when multiple concurrent calls occur, since no synchronization is performed.
-
firstEntry
Answers the entry with the smallest key, or null if the map is empty.- Specified by:
firstEntryin interfaceNavigableMap<K,V> - Returns:
- the entry with the smallest key, or null if the map is empty
- Since:
- 1.6
- See Also:
-
lastEntry
-
pollFirstEntry
Deletes and answers the entry with the smallest key, or null if the map is empty.- Specified by:
pollFirstEntryin interfaceNavigableMap<K,V> - Returns:
- the entry with the smallest key, or null if the map is empty
- Since:
- 1.6
- See Also:
-
pollLastEntry
Deletes and answers the entry with the biggest key, or null if the map is empty.- Specified by:
pollLastEntryin interfaceNavigableMap<K,V> - Returns:
- the entry with the biggest key, or null if the map is empty
- Since:
- 1.6
- See Also:
-
higherEntry
Answers an entry related with the smallest key greater than the specified key, or null if no such key.- Specified by:
higherEntryin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- the entry, or null if no such key
- Since:
- 1.6
- See Also:
-
higherKey
-
lowerEntry
Answers an entry related with the biggest key less than the specified key, or null if no such key.- Specified by:
lowerEntryin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- the entry, or null if no such key
- Since:
- 1.6
- See Also:
-
lowerKey
-
ceilingEntry
Answers an entry related with the smallest key greater than or equal to the specified key, or null if no such key.- Specified by:
ceilingEntryin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- the entry, or null if no such key
- Since:
- 1.6
- See Also:
-
ceilingKey
Answers the smallest key greater than or equal to the specified key, or null if no such key.- Specified by:
ceilingKeyin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- the smallest key greater than or equal to key, or null if no such key
- Since:
- 1.6
- See Also:
-
floorEntry
Answers an entry related with the biggest key less than or equal to the specified key, or null if no such key.- Specified by:
floorEntryin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- the entry, or null if no such key
- Since:
- 1.6
- See Also:
-
floorKey
-
entrySet
Returns a set containing all of the mappings in this map. Each mapping is an instance ofMap.Entry. As the set is backed by this map, changes in one will be reflected in the other. It does not support adding operations. -
descendingKeySet
Answers a NavigableSet view of the keys in descending order.- Specified by:
descendingKeySetin interfaceNavigableMap<K,V> - Returns:
- the navigable set view
- Since:
- 1.6
- See Also:
-
descendingMap
Answers a reverse order view of the map.- Specified by:
descendingMapin interfaceNavigableMap<K,V> - Returns:
- the reverse order view of the map
- Since:
- 1.6
- See Also:
-
subMap
Answers a view of part of the map whose keys is from startKey to endKey.- Specified by:
subMapin interfaceNavigableMap<K,V> - Parameters:
start- the start keystartInclusive- true if the start key is in the returned mapend- the end keyendInclusive- true if the end key is in the returned map- Returns:
- the sub-map view
- Since:
- 1.6
- See Also:
-
headMap
Answers a view of the head of the map whose keys are smaller than (or equal to, depends on inclusive argument) endKey.- Specified by:
headMapin interfaceNavigableMap<K,V> - Parameters:
end- the end keyinclusive- true if the end key is in the returned map- Returns:
- the head-map view
- Since:
- 1.6
- See Also:
-
tailMap
Answers a view of the tail of the map whose keys are bigger than (or equal to, depends on inclusive argument) startKey.- Specified by:
tailMapin interfaceNavigableMap<K,V> - Parameters:
start- the start keyinclusive- true if the start key is in the returned map- Returns:
- the tail-map view
- Since:
- 1.6
- See Also:
-
subMap
Returns a sorted map over a range of this sorted map with all keys greater than or equal to the specifiedstartKeyand less than the specifiedendKey. Changes to the returned sorted map are reflected in this sorted map and vice versa.Note: The returned map will not allow an insertion of a key outside the specified range.
- Specified by:
subMapin interfaceSortedMap<K,V> - Parameters:
startKey- the low boundary of the range (inclusive).endKey- the high boundary of the range (exclusive),- Returns:
- a sorted map with the key from the specified range.
- Throws:
ClassCastException- if the start or end key cannot be compared with the keys in this map.NullPointerException- if the start or end key isnulland the comparator cannot handlenullkeys.IllegalArgumentException- if the start key is greater than the end key, or if this map is itself a sorted map over a range of another sorted map and the specified range is outside of its range.
-
headMap
Returns a sorted map over a range of this sorted map with all keys that are less than the specifiedendKey. Changes to the returned sorted map are reflected in this sorted map and vice versa.Note: The returned map will not allow an insertion of a key outside the specified range.
- Specified by:
headMapin interfaceSortedMap<K,V> - Parameters:
endKey- the high boundary of the range specified.- Returns:
- a sorted map where the keys are less than
endKey. - Throws:
ClassCastException- if the specified key cannot be compared with the keys in this map.NullPointerException- if the specified key isnulland the comparator cannot handlenullkeys.IllegalArgumentException- if this map is itself a sorted map over a range of another map and the specified key is outside of its range.
-
tailMap
Returns a sorted map over a range of this sorted map with all keys that are greater than or equal to the specifiedstartKey. Changes to the returned sorted map are reflected in this sorted map and vice versa.Note: The returned map will not allow an insertion of a key outside the specified range.
- Specified by:
tailMapin interfaceSortedMap<K,V> - Parameters:
startKey- the low boundary of the range specified.- Returns:
- a sorted map where the keys are greater or equal to
startKey. - Throws:
ClassCastException- if the specified key cannot be compared with the keys in this map.NullPointerException- if the specified key isnulland the comparator cannot handlenullkeys.IllegalArgumentException- if this map itself a sorted map over a range of another map and the specified key is outside of its range.
-