java.lang.Object | ||
↳ | java.util.AbstractMap<K, V> | |
↳ | java.util.TreeMap<K, V> |
TreeMap is an implementation of SortedMap. All optional operations (adding and removing) are supported. 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.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new empty
TreeMap instance. | |||||||||||
Constructs a new empty
TreeMap instance with the specified
comparator. | |||||||||||
Constructs a new
TreeMap instance containing the mappings from
the specified map and using natural ordering. | |||||||||||
Constructs a new
TreeMap instance containing the mappings from
the specified SortedMap and using the same comparator. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Removes all mappings from this TreeMap, leaving it empty.
| |||||||||||
Returns a new
TreeMap with the same mappings, size and comparator
as this instance. | |||||||||||
Returns the comparator used to compare elements in this map.
| |||||||||||
Returns whether this map contains the specified key.
| |||||||||||
Returns whether this map contains the specified value.
| |||||||||||
Returns a set containing all of the mappings in this map.
| |||||||||||
Returns the first key in this map.
| |||||||||||
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 specified
endKey . | |||||||||||
Returns a set of the keys contained in this map.
| |||||||||||
Returns the last key in this map.
| |||||||||||
Maps the specified key to the specified value.
| |||||||||||
Copies all the mappings in the given map to this map.
| |||||||||||
Removes the mapping with the specified key from this map.
| |||||||||||
Returns the number of mappings in this map.
| |||||||||||
Returns a sorted map over a range of this sorted map with all keys
greater than or equal to the specified
startKey and less than the
specified endKey . | |||||||||||
Returns a sorted map over a range of this sorted map with all keys that
are greater than or equal to the specified
startKey . | |||||||||||
Returns a collection of the values contained in this map.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class java.util.AbstractMap
| |||||||||||
From class java.lang.Object
| |||||||||||
From interface java.util.Map
| |||||||||||
From interface java.util.SortedMap
|
Constructs a new empty TreeMap
instance.
Constructs a new empty TreeMap
instance with the specified
comparator.
comparator | the comparator to compare keys with. |
---|
Constructs a new TreeMap
instance containing the mappings from
the specified map and using natural ordering.
map | the mappings to add. |
---|
ClassCastException | if a key in the specified map does not implement the Comparable interface, or if the keys in the map cannot be compared. |
---|
Constructs a new TreeMap
instance containing the mappings from
the specified SortedMap and using the same comparator.
map | the mappings to add. |
---|
Returns a new TreeMap
with the same mappings, size and comparator
as this instance.
Returns the comparator used to compare elements in this map.
null
if the natural ordering is used.Returns whether this map contains the specified key.
key | the key to search for. |
---|
true
if this map contains the specified key,
false
otherwise.Returns whether this map contains the specified value.
value | the value to search for. |
---|
true
if this map contains the specified value,
false
otherwise.Returns a set containing all of the mappings in this map. Each mapping is an instance of Map.Entry. As the set is backed by this map, changes in one will be reflected in the other. It does not support adding operations.
Returns the first key in this map.
NoSuchElementException | if this sorted map is empty. |
---|
Returns the value of the mapping with the specified key.
key | the key. |
---|
ClassCastException | if the key cannot be compared with the keys in this map. |
---|---|
NullPointerException | if the key is null and the comparator cannot handle
null . |
Returns a sorted map over a range of this sorted map with all keys that
are less than the specified endKey
. 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.
endKey | the high boundary of the range specified. |
---|
endKey
.ClassCastException | if the specified key cannot be compared with the keys in this map. |
---|---|
NullPointerException | if the specified key is null and the comparator
cannot handle null keys. |
IllegalArgumentException | if this map is itself a sorted map over a range of another map and the specified key is outside of its range. |
Returns a set of the keys contained in this map. The set is backed by this map so changes to one are reflected by the other. The set does not support adding.
Returns the last key in this map.
NoSuchElementException | if this map is empty. |
---|
Maps the specified key to the specified value.
key | the key. |
---|---|
value | the value. |
null
if there was no mapping.ClassCastException | if the specified key cannot be compared with the keys in this map. |
---|---|
NullPointerException | if the specified key is null and the comparator
cannot handle null keys. |
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.
map | the map to copy mappings from. |
---|
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 is null and the
comparator cannot handle null keys. |
Removes the mapping with the specified key from this map.
key | the key of the mapping to remove. |
---|
null
if no mapping
for the specified key was found.ClassCastException | if the specified key cannot be compared with the keys in this map. |
---|---|
NullPointerException | if the specified key is null and the comparator
cannot handle null keys. |
Returns the number of mappings in this map.
Returns a sorted map over a range of this sorted map with all keys
greater than or equal to the specified startKey
and less than the
specified endKey
. 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.
startKey | the low boundary of the range (inclusive). |
---|---|
endKey | the high boundary of the range (exclusive), |
ClassCastException | if the start or end key cannot be compared with the keys in this map. |
---|---|
NullPointerException | if the start or end key is null and the comparator
cannot handle null keys. |
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. |
Returns a sorted map over a range of this sorted map with all keys that
are greater than or equal to the specified startKey
. 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.
startKey | the low boundary of the range specified. |
---|
startKey
.ClassCastException | if the specified key cannot be compared with the keys in this map. |
---|---|
NullPointerException | if the specified key is null and the comparator
cannot handle null keys. |
IllegalArgumentException | if this map itself a sorted map over a range of another map and the specified key is outside of its range. |
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 size
method wraps the map's size method and the contains
method 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.