K
- type of keyV
- type of valuepublic class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
TreeMap()
Constructs a new empty
TreeMap instance. |
TreeMap(Comparator<? super K> comparator)
Constructs a new empty
TreeMap instance with the specified
comparator. |
TreeMap(Map<? extends K,? extends V> map)
Constructs a new
TreeMap instance containing the mappings from
the specified map and using natural ordering. |
TreeMap(SortedMap<K,? extends V> map)
Constructs a new
TreeMap instance containing the mappings from
the specified SortedMap and using the same comparator. |
Modifier and Type | Method and Description |
---|---|
Map.Entry<K,V> |
ceilingEntry(K key)
Answers an entry related with the smallest key greater than or equal to
the specified key, or null if no such key.
|
K |
ceilingKey(K key)
Answers the smallest key greater than or equal to the specified key, or
null if no such key.
|
void |
clear()
Removes all mappings from this TreeMap, leaving it empty.
|
Comparator<? super K> |
comparator()
Returns the comparator used to compare elements in this map.
|
boolean |
containsKey(Object key)
Returns whether this map contains the specified key.
|
boolean |
containsValue(Object value)
Returns whether this map contains the specified value.
|
NavigableSet<K> |
descendingKeySet()
Answers a NavigableSet view of the keys in descending order.
|
NavigableMap<K,V> |
descendingMap()
Answers a reverse order view of the map.
|
Set<Map.Entry<K,V>> |
entrySet()
Returns a set containing all of the mappings in this map.
|
Map.Entry<K,V> |
firstEntry()
Answers the entry with the smallest key, or null if the map is empty.
|
K |
firstKey()
Returns the first key in this map.
|
Map.Entry<K,V> |
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.
|
K |
floorKey(K key)
Answers the biggest key less than or equal to the specified key, or null
if no such key.
|
V |
get(Object key)
Returns the value of the mapping with the specified key.
|
SortedMap<K,V> |
headMap(K endKey)
Returns a sorted map over a range of this sorted map with all keys that
are less than the specified
endKey . |
NavigableMap<K,V> |
headMap(K end,
boolean inclusive)
Answers a view of the head of the map whose keys are smaller than (or
equal to, depends on inclusive argument) endKey.
|
Map.Entry<K,V> |
higherEntry(K key)
Answers an entry related with the smallest key greater than the specified
key, or null if no such key.
|
K |
higherKey(K key)
Answers the smallest key greater than the specified key, or null if no
such key.
|
Set<K> |
keySet()
Returns a set of the keys contained in this map.
|
Map.Entry<K,V> |
lastEntry()
Answers the entry with the biggest key, or null if the map is empty.
|
K |
lastKey()
Returns the last key in this map.
|
Map.Entry<K,V> |
lowerEntry(K key)
Answers an entry related with the biggest key less than the specified
key, or null if no such key.
|
K |
lowerKey(K key)
Answers the biggest key less than the specified key, or null if no such
key.
|
NavigableSet<K> |
navigableKeySet()
Answers a NavigableSet view of the keys in ascending order.
|
Map.Entry<K,V> |
pollFirstEntry()
Deletes and answers the entry with the smallest key, or null if the map
is empty.
|
Map.Entry<K,V> |
pollLastEntry()
Deletes and answers the entry with the biggest key, or null if the map is
empty.
|
V |
put(K key,
V value)
Maps the specified key to the specified value.
|
void |
putAll(Map<? extends K,? extends V> map)
Copies all the mappings in the given map to this map.
|
V |
remove(Object key)
Removes the mapping with the specified key from this map.
|
int |
size()
Returns the number of mappings in this map.
|
NavigableMap<K,V> |
subMap(K start,
boolean startInclusive,
K end,
boolean endInclusive)
Answers a view of part of the map whose keys is from startKey to endKey.
|
SortedMap<K,V> |
subMap(K startKey,
K endKey)
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 . |
SortedMap<K,V> |
tailMap(K startKey)
Returns a sorted map over a range of this sorted map with all keys that
are greater than or equal to the specified
startKey . |
NavigableMap<K,V> |
tailMap(K start,
boolean inclusive)
Answers a view of the tail of the map whose keys are bigger than (or
equal to, depends on inclusive argument) startKey.
|
Collection<V> |
values()
Returns a collection of the values contained in this map.
|
equals, hashCode, isEmpty, toString
public TreeMap()
TreeMap
instance.public TreeMap(Comparator<? super K> comparator)
TreeMap
instance with the specified
comparator.comparator
- the comparator to compare keys with.public TreeMap(Map<? extends K,? extends V> map)
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.public void clear()
clear
in interface Map<K,V>
clear
in class AbstractMap<K,V>
Map.isEmpty()
,
size()
public Comparator<? super K> comparator()
comparator
in interface SortedMap<K,V>
null
if the natural ordering is used.public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
key
- the key to search for.true
if this map contains the specified key,
false
otherwise.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.public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
value
- the value to search for.true
if this map contains the specified value,
false
otherwise.public K firstKey()
firstKey
in interface SortedMap<K,V>
NoSuchElementException
- if this map is empty.public V get(Object key)
get
in interface Map<K,V>
get
in class AbstractMap<K,V>
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
.public Set<K> keySet()
public K lastKey()
lastKey
in interface SortedMap<K,V>
NoSuchElementException
- if this map is empty.public V put(K key, V value)
put
in interface Map<K,V>
put
in class AbstractMap<K,V>
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.public void putAll(Map<? extends K,? extends V> map)
putAll
in interface Map<K,V>
putAll
in class AbstractMap<K,V>
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.public V remove(Object key)
remove
in interface Map<K,V>
remove
in class AbstractMap<K,V>
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.public int size()
public Collection<V> values()
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.
public Map.Entry<K,V> firstEntry()
firstEntry
in interface NavigableMap<K,V>
NavigableMap.firstEntry()
public Map.Entry<K,V> lastEntry()
lastEntry
in interface NavigableMap<K,V>
NavigableMap.lastEntry()
public Map.Entry<K,V> pollFirstEntry()
pollFirstEntry
in interface NavigableMap<K,V>
NavigableMap.pollFirstEntry()
public Map.Entry<K,V> pollLastEntry()
pollLastEntry
in interface NavigableMap<K,V>
NavigableMap.pollLastEntry()
public Map.Entry<K,V> higherEntry(K key)
higherEntry
in interface NavigableMap<K,V>
key
- the keyNavigableMap.higherEntry(Object)
public K higherKey(K key)
higherKey
in interface NavigableMap<K,V>
key
- the keyNavigableMap.higherKey(Object)
public Map.Entry<K,V> lowerEntry(K key)
lowerEntry
in interface NavigableMap<K,V>
key
- the keyNavigableMap.lowerEntry(Object)
public K lowerKey(K key)
lowerKey
in interface NavigableMap<K,V>
key
- the keyNavigableMap.lowerKey(Object)
public Map.Entry<K,V> ceilingEntry(K key)
ceilingEntry
in interface NavigableMap<K,V>
key
- the keyNavigableMap.ceilingEntry(java.lang.Object)
public K ceilingKey(K key)
ceilingKey
in interface NavigableMap<K,V>
key
- the keyNavigableMap.ceilingKey(java.lang.Object)
public Map.Entry<K,V> floorEntry(K key)
floorEntry
in interface NavigableMap<K,V>
key
- the keyNavigableMap.floorEntry(Object)
public K floorKey(K key)
floorKey
in interface NavigableMap<K,V>
key
- the keyNavigableMap.floorKey(Object)
public Set<Map.Entry<K,V>> entrySet()
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.public NavigableSet<K> navigableKeySet()
navigableKeySet
in interface NavigableMap<K,V>
NavigableMap.navigableKeySet()
public NavigableSet<K> descendingKeySet()
descendingKeySet
in interface NavigableMap<K,V>
NavigableMap.descendingKeySet()
public NavigableMap<K,V> descendingMap()
descendingMap
in interface NavigableMap<K,V>
NavigableMap.descendingMap()
public NavigableMap<K,V> subMap(K start, boolean startInclusive, K end, boolean endInclusive)
subMap
in interface NavigableMap<K,V>
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 mapNavigableMap.subMap(Object, boolean, Object, boolean)
public NavigableMap<K,V> headMap(K end, boolean inclusive)
headMap
in interface NavigableMap<K,V>
end
- the end keyinclusive
- true if the end key is in the returned mapNavigableMap.headMap(Object, boolean)
public NavigableMap<K,V> tailMap(K start, boolean inclusive)
tailMap
in interface NavigableMap<K,V>
start
- the start keyinclusive
- true if the start key is in the returned mapNavigableMap.tailMap(Object, boolean)
public SortedMap<K,V> subMap(K startKey, K endKey)
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.
subMap
in interface SortedMap<K,V>
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.public SortedMap<K,V> headMap(K endKey)
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.
headMap
in interface SortedMap<K,V>
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.public SortedMap<K,V> tailMap(K startKey)
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.
tailMap
in interface SortedMap<K,V>
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.