- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
Serializable,Cloneable,Map<K,,V> NavigableMap<K,,V> SortedMap<K,V>
NavigableMap implementation.- See Also:
-
Nested Class Summary
Nested classes/interfaces declared in class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorsConstructorDescriptionTreeMap()Constructs a new empty instance of TreeMap.TreeMap(Comparator<? super K> theComparator) Constructs a new empty instance of TreeMap which uses the specified Comparator.Constructs a new instance of TreeMap containing the mappings from the specified Map and using the natural ordering.Constructs a new instance of TreeMap containing the mappings from the specified SortedMap and using the same Comparator. -
Method Summary
Modifier and TypeMethodDescriptionceilingEntry(K key) Returns a key-value mapping associated with the least key greater than or equal to the given key, ornullif there is no such key.ceilingKey(K key) Returns the least key greater than or equal to the given key, ornullif there is no such key.voidclear()Removes all mappings from this TreeMap, leaving it empty.clone()Answers a new TreeMap with the same mappings, size and comparator as this TreeMap.Comparator<? super K>Answers the Comparator used to compare elements in this TreeMap.booleancontainsKey(Object key) Searches this TreeMap for the specified key.booleancontainsValue(Object value) Searches this TreeMap for the specified value.Returns a reverse orderNavigableSetview of the keys contained in this map.Returns a reverse order view of the mappings contained in this map.entrySet()Returns aSetview of the mappings contained in this map.Returns a key-value mapping associated with the least key in this map, ornullif the map is empty.firstKey()Answer the first sorted key in this TreeMap.floorEntry(K key) Returns a key-value mapping associated with the greatest key less than or equal to the given key, ornullif there is no such key.Returns the greatest key less than or equal to the given key, ornullif there is no such key.Answers the value of the mapping with the specified key.Returns a view of the portion of this map whose keys are strictly less thantoKey.Returns a view of the portion of this map whose keys are less than (or equal to, ifinclusiveis true)toKey.higherEntry(K key) Returns a key-value mapping associated with the least key strictly greater than the given key, ornullif there is no such key.Returns the least key strictly greater than the given key, ornullif there is no such key.keySet()Answers a Set of the keys contained in this TreeMap.Returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty.lastKey()Answer the last sorted key in this TreeMap.lowerEntry(K key) Returns a key-value mapping associated with the greatest key strictly less than the given key, ornullif there is no such key.Returns the greatest key strictly less than the given key, ornullif there is no such key.Returns aNavigableSetview of the keys contained in this map.Removes and returns a key-value mapping associated with the least key in this map, ornullif the map is empty.Removes and returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty.Maps the specified key to the specified value.voidCopies every mapping in the specified Map to this TreeMap.Removes a mapping with the specified key from this TreeMap.intsize()Answers the number of mappings in this TreeMap.Returns a view of the portion of this map whose keys range fromfromKeytotoKey.Returns a view of the portion of this map whose keys range fromfromKey, inclusive, totoKey, exclusive.Returns a view of the portion of this map whose keys are greater than or equal tofromKey.Returns a view of the portion of this map whose keys are greater than (or equal to, ifinclusiveis true)fromKey.values()Returns aCollectionview of the values contained in this map.Methods declared in interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, isEmpty, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
TreeMap
public TreeMap()Constructs a new empty instance of TreeMap. -
TreeMap
Constructs a new empty instance of TreeMap which uses the specified Comparator.- Parameters:
theComparator- the Comparator
-
TreeMap
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
-
TreeMap
Constructs a new instance of TreeMap containing the mappings from the specified SortedMap and using the same Comparator.- Parameters:
map- the mappings to add
-
-
Method Details
-
clear
public void clear()Removes all mappings from this TreeMap, leaving it empty.- Specified by:
clearin interfaceMap<K,V> - Overrides:
clearin classAbstractMap<K,V> - See Also:
-
Map.isEmpty()size
-
clone
Answers a new TreeMap with the same mappings, size and comparator as this TreeMap.- Overrides:
clonein classAbstractMap<K,V> - Returns:
- a shallow copy of this TreeMap
- See Also:
-
comparator
Answers the Comparator used to compare elements in this TreeMap.- Specified by:
comparatorin interfaceSortedMap<K,V> - Returns:
- a Comparator or null if the natural ordering is used
-
containsKey
Searches this TreeMap for the specified key.- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V> - Parameters:
key- the object to search for- Returns:
- true if
keyis a key of this TreeMap, false otherwise - Throws:
ClassCastException- when the key cannot be compared with the keys in this TreeMapNullPointerException- when the key is null and the comparator cannot handle null
-
containsValue
Searches this TreeMap for the specified value.- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V> - Parameters:
value- the object to search for- Returns:
- true if
valueis a value of this TreeMap, false otherwise
-
firstKey
Answer the first sorted key in this TreeMap.- Specified by:
firstKeyin interfaceSortedMap<K,V> - Returns:
- the first sorted key
- Throws:
NoSuchElementException- when this TreeMap is empty
-
get
Answers 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- when the key cannot be compared with the keys in this TreeMapNullPointerException- when the key is null and the comparator cannot handle null
-
keySet
Answers 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. -
lastKey
Answer the last sorted key in this TreeMap.- Specified by:
lastKeyin interfaceSortedMap<K,V> - Returns:
- the last sorted key
- Throws:
NoSuchElementException- when this TreeMap is empty
-
put
Maps the specified key to the specified value.- Specified by:
putin interfaceMap<K,V> - Overrides:
putin classAbstractMap<K,V> - Parameters:
key- the keyvalue- 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 TreeMapNullPointerException- when the key is null and the comparator cannot handle null
-
putAll
Copies every mapping in the specified Map to this TreeMap.- Specified by:
putAllin interfaceMap<K,V> - Overrides:
putAllin classAbstractMap<K,V> - Parameters:
map- the Map to copy mappings from- Throws:
ClassCastException- when a key in the Map cannot be compared with the keys in this TreeMapNullPointerException- when a key in the Map is null and the comparator cannot handle null
-
remove
Removes a mapping with the specified key from this TreeMap.- 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 null if key is not a key in this TreeMap
- Throws:
ClassCastException- when the key cannot be compared with the keys in this TreeMapNullPointerException- when the key is null and the comparator cannot handle null
-
size
public int size()Answers the number of mappings in this TreeMap. -
firstEntry
Returns a key-value mapping associated with the least key in this map, ornullif the map is empty.- Specified by:
firstEntryin interfaceNavigableMap<K,V> - Returns:
- an entry with the least key,
or
nullif this map is empty - Since:
- 1.6
- See Also:
-
lastEntry
Returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty.- Specified by:
lastEntryin interfaceNavigableMap<K,V> - Returns:
- an entry with the greatest key,
or
nullif this map is empty - Since:
- 1.6
- See Also:
-
pollFirstEntry
Removes and returns a key-value mapping associated with the least key in this map, ornullif the map is empty.- Specified by:
pollFirstEntryin interfaceNavigableMap<K,V> - Returns:
- the removed first entry of this map,
or
nullif this map is empty - Since:
- 1.6
- See Also:
-
pollLastEntry
Removes and returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty.- Specified by:
pollLastEntryin interfaceNavigableMap<K,V> - Returns:
- the removed last entry of this map,
or
nullif this map is empty - Since:
- 1.6
- See Also:
-
higherEntry
Returns a key-value mapping associated with the least key strictly greater than the given key, ornullif there is no such key.- Specified by:
higherEntryin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- an entry with the least key greater than
key, ornullif there is no such key - Since:
- 1.6
- See Also:
-
higherKey
Returns the least key strictly greater than the given key, ornullif there is no such key.- Specified by:
higherKeyin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- the least key greater than
key, ornullif there is no such key - Since:
- 1.6
- See Also:
-
lowerEntry
Returns a key-value mapping associated with the greatest key strictly less than the given key, ornullif there is no such key.- Specified by:
lowerEntryin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- an entry with the greatest key less than
key, ornullif there is no such key - Since:
- 1.6
- See Also:
-
lowerKey
Returns the greatest key strictly less than the given key, ornullif there is no such key.- Specified by:
lowerKeyin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- the greatest key less than
key, ornullif there is no such key - Since:
- 1.6
- See Also:
-
ceilingEntry
Returns a key-value mapping associated with the least key greater than or equal to the given key, ornullif there is no such key.- Specified by:
ceilingEntryin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- an entry with the least key greater than or equal to
key, ornullif there is no such key - Since:
- 1.6
- See Also:
-
ceilingKey
Returns the least key greater than or equal to the given key, ornullif there is no such key.- Specified by:
ceilingKeyin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- the least key greater than or equal to
key, ornullif there is no such key - Since:
- 1.6
- See Also:
-
floorEntry
Description copied from interface:NavigableMapReturns a key-value mapping associated with the greatest key less than or equal to the given key, ornullif there is no such key.- Specified by:
floorEntryin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- an entry with the greatest key less than or equal to
key, ornullif there is no such key
-
floorKey
Description copied from interface:NavigableMapReturns the greatest key less than or equal to the given key, ornullif there is no such key.- Specified by:
floorKeyin interfaceNavigableMap<K,V> - Parameters:
key- the key- Returns:
- the greatest key less than or equal to
key, ornullif there is no such key
-
entrySet
Description copied from interface:SortedMapReturns aSetview of the mappings contained in this map. The set's iterator returns the entries in ascending key order. 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 (except through the iterator's ownremoveoperation, or through thesetValueoperation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via theIterator.remove,Set.remove,removeAll,retainAllandclearoperations. It does not support theaddoraddAlloperations. -
descendingKeySet
Description copied from interface:NavigableMapReturns a reverse orderNavigableSetview of the keys contained in this map. The set's iterator returns the keys in descending order. 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 (except through the iterator's ownremoveoperation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via theIterator.remove,Set.remove,removeAll,retainAll, andclearoperations. It does not support theaddoraddAlloperations.- Specified by:
descendingKeySetin interfaceNavigableMap<K,V> - Returns:
- a reverse order navigable set view of the keys in this map
-
descendingMap
Description copied from interface:NavigableMapReturns a reverse order view of the mappings contained in this map. The descending map is backed by this map, so changes to the map are reflected in the descending map, and vice-versa. If either map is modified while an iteration over a collection view of either map is in progress (except through the iterator's ownremoveoperation), the results of the iteration are undefined.The returned map has an ordering equivalent to
Collections.reverseOrder(comparator()). The expressionm.descendingMap().descendingMap()returns a view ofmessentially equivalent tom.- Specified by:
descendingMapin interfaceNavigableMap<K,V> - Returns:
- a reverse order view of this map
-
subMap
Description copied from interface:NavigableMapReturns a view of the portion of this map whose keys range fromfromKeytotoKey. IffromKeyandtoKeyare equal, the returned map is empty unlessfromInclusiveandtoInclusiveare both true. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.The returned map will throw an
IllegalArgumentExceptionon an attempt to insert a key outside of its range, or to construct a submap either of whose endpoints lie outside its range.- Specified by:
subMapin interfaceNavigableMap<K,V> - Parameters:
fromKey- low endpoint of the keys in the returned mapfromInclusive-trueif the low endpoint is to be included in the returned viewtoKey- high endpoint of the keys in the returned maptoInclusive-trueif the high endpoint is to be included in the returned view- Returns:
- a view of the portion of this map whose keys range from
fromKeytotoKey
-
headMap
Description copied from interface:NavigableMapReturns a view of the portion of this map whose keys are less than (or equal to, ifinclusiveis true)toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.The returned map will throw an
IllegalArgumentExceptionon an attempt to insert a key outside its range.- Specified by:
headMapin interfaceNavigableMap<K,V> - Parameters:
toKey- high endpoint of the keys in the returned mapinclusive-trueif the high endpoint is to be included in the returned view- Returns:
- a view of the portion of this map whose keys are less than
(or equal to, if
inclusiveis true)toKey
-
tailMap
Description copied from interface:NavigableMapReturns a view of the portion of this map whose keys are greater than (or equal to, ifinclusiveis true)fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.The returned map will throw an
IllegalArgumentExceptionon an attempt to insert a key outside its range.- Specified by:
tailMapin interfaceNavigableMap<K,V> - Parameters:
fromKey- low endpoint of the keys in the returned mapinclusive-trueif the low endpoint is to be included in the returned view- Returns:
- a view of the portion of this map whose keys are greater than
(or equal to, if
inclusiveis true)fromKey
-
subMap
Description copied from interface:NavigableMapReturns a view of the portion of this map whose keys range fromfromKey, inclusive, totoKey, exclusive. (IffromKeyandtoKeyare equal, the returned map is empty.) The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.The returned map will throw an
IllegalArgumentExceptionon an attempt to insert a key outside its range.Equivalent to
subMap(fromKey, true, toKey, false).- Specified by:
subMapin interfaceNavigableMap<K,V> - Specified by:
subMapin interfaceSortedMap<K,V> - Parameters:
fromKey- low endpoint (inclusive) of the keys in the returned maptoKey- high endpoint (exclusive) of the keys in the returned map- Returns:
- a view of the portion of this map whose keys range from
fromKey, inclusive, totoKey, exclusive
-
headMap
Description copied from interface:NavigableMapReturns a view of the portion of this map whose keys are strictly less thantoKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.The returned map will throw an
IllegalArgumentExceptionon an attempt to insert a key outside its range.Equivalent to
headMap(toKey, false).- Specified by:
headMapin interfaceNavigableMap<K,V> - Specified by:
headMapin interfaceSortedMap<K,V> - Parameters:
toKey- high endpoint (exclusive) of the keys in the returned map- Returns:
- a view of the portion of this map whose keys are strictly
less than
toKey - Throws:
ClassCastException- iftoKeyis not compatible with this map's comparator (or, if the map has no comparator, iftoKeydoes not implementComparable). Implementations may, but are not required to, throw this exception iftoKeycannot be compared to keys currently in the map.NullPointerException- iftoKeyis null and this map uses natural ordering, or its comparator does not permit null keysIllegalArgumentException- if this map itself has a restricted range, andtoKeylies outside the bounds of the range
-
tailMap
Description copied from interface:NavigableMapReturns a view of the portion of this map whose keys are greater than or equal tofromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.The returned map will throw an
IllegalArgumentExceptionon an attempt to insert a key outside its range.Equivalent to
tailMap(fromKey, true).- Specified by:
tailMapin interfaceNavigableMap<K,V> - Specified by:
tailMapin interfaceSortedMap<K,V> - Parameters:
fromKey- low endpoint (inclusive) of the keys in the returned map- Returns:
- a view of the portion of this map whose keys are greater
than or equal to
fromKey - Throws:
ClassCastException- iffromKeyis not compatible with this map's comparator (or, if the map has no comparator, iffromKeydoes not implementComparable). Implementations may, but are not required to, throw this exception iffromKeycannot be compared to keys currently in the map.NullPointerException- iffromKeyis null and this map uses natural ordering, or its comparator does not permit null keysIllegalArgumentException- if this map itself has a restricted range, andfromKeylies outside the bounds of the range
-
values
Description copied from class:AbstractMapReturns aCollectionview 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 (except through the iterator's ownremoveoperation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via theIterator.remove,Collection.remove,removeAll,retainAllandclearoperations. It does not support theaddoraddAlloperations.
-