public class TreeMap<K, V>
- Object
- AbstractMap<K, V>
- TreeMap
ImplementsMap<K, V>, NavigableMap<K, V>, SortedMap<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
Type parameter K: type of key
Type parameter V: type of value
Constructors
public TreeMap() | Constructs a new empty TreeMap instance. |
public TreeMap(Comparator<? super K> comparator) | Constructs a new empty TreeMap instance with the specified comparator. |
public TreeMap(Map<? extends K, ? extends V> map) | Constructs a new TreeMap instance containing the mappings from the specified map and using natural ordering. |
public TreeMap(SortedMap<K, ? extends V> map) | Constructs a new TreeMap instance containing the mappings from the specified SortedMap and using the same comparator. |
Methods
public void clear() | Removes all mappings from this TreeMap, leaving it empty. |
public Comparator<? super K> comparator() | Returns the comparator used to compare elements in this map. |
public boolean containsKey(Object key) | Returns whether this map contains the specified key. |
public boolean containsValue(Object value) | Returns whether this map contains the specified value. |
public K firstKey() | Returns the first key in this map. |
public V get(Object key) | Returns the value of the mapping with the specified key. |
public Set<K> keySet() | Returns a set of the keys contained in this map. |
public K lastKey() | Returns the last key in this map. |
public V put(K key, V value) | Maps the specified key to the specified value. |
public void putAll(Map<? extends K, ? extends V> map) | Copies all the mappings in the given map to this map. |
public V remove(Object key) | Removes the mapping with the specified key from this map. |
public int size() | Returns the number of mappings in this map. |
public Collection<V> values() | Returns a collection of the values contained in this map. |
public Map.Entry<K, V> firstEntry() | Answers the entry with the smallest key, or null if the map is empty. |
public Map.Entry<K, V> lastEntry() | Answers the entry with the biggest key, or null if the map is empty. |
public Map.Entry<K, V> pollFirstEntry() | Deletes and answers the entry with the smallest key, or null if the map is empty. |
public Map.Entry<K, V> pollLastEntry() | Deletes and answers the entry with the biggest key, or null if the map is empty. |
public 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. |
public K higherKey(K key) | Answers the smallest key greater than the specified key, or null if no such key. |
public 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. |
public K lowerKey(K key) | Answers the biggest key less than the specified key, or null if no such key. |
public 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. |
public K ceilingKey(K key) | Answers the smallest key greater than or equal to the specified key, or null if no such key. |
public 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. |
public K floorKey(K key) | Answers the biggest key less than or equal to the specified key, or null if no such key. |
public Set<Map.Entry<K, V>> entrySet() | Returns a set containing all of the mappings in this map. |
public NavigableSet<K> navigableKeySet() | Answers a NavigableSet view of the keys in ascending order. |
public NavigableSet<K> descendingKeySet() | Answers a NavigableSet view of the keys in descending order. |
public NavigableMap<K, V> descendingMap() | Answers a reverse order view of the map. |
public 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. |
public 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. |
public 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. |
public 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. |
public 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. |
public 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. |
Inherited nested types
Inherited methods
From AbstractMap
Constructor details
TreeMap
public TreeMap()TreeMap instance.TreeMap
public TreeMap(Comparator<? super K> comparator)TreeMap instance with the specified
comparator.Parameters
comparatorComparator<? super K>- the comparator to compare keys with.
TreeMap
public TreeMap(Map<? extends K, ? extends V> map)TreeMap instance containing the mappings from
the specified map and using natural ordering.Parameters
mapMap<? extends K, ? extends V>- 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
public TreeMap(SortedMap<K, ? extends V> map)TreeMap instance containing the mappings from
the specified SortedMap and using the same comparator.Parameters
mapSortedMap<K, ? extends V>- the mappings to add.
Method details
clear
public void clear()Throws
UnsupportedOperationException- if removing from this map is not supported.
See also
comparator
public Comparator<? super K> comparator()Returns
null if the natural ordering is used.containsKey
public boolean containsKey(Object key)Parameters
keyObject- the key to search for.
Returns
true if this map contains the specified key,
false otherwise.Throws
ClassCastException- if the specified key cannot be compared with the keys in this map.
NullPointerException- if the specified key is
nulland the comparator cannot handlenullkeys.
containsValue
public boolean containsValue(Object value)Parameters
valueObject- the value to search for.
Returns
true if this map contains the specified value,
false otherwise.firstKey
public K firstKey()Returns
Throws
NoSuchElementException- if this map is empty.
get
public V get(Object key)Parameters
keyObject- the key.
Returns
Throws
ClassCastException- if the key cannot be compared with the keys in this map.
NullPointerException- if the key is
nulland the comparator cannot handlenull.
keySet
public Set<K> keySet()Returns
lastKey
public K lastKey()Returns
Throws
NoSuchElementException- if this map is empty.
put
public V put(K key, V value)Parameters
keyK- the key.
valueV- the value.
Returns
null if there was no mapping.Throws
ClassCastException- if the specified key cannot be compared with the keys in this map.
NullPointerException- if the specified key is
nulland the comparator cannot handlenullkeys.
putAll
public void putAll(Map<? extends K, ? extends V> map)Parameters
mapMap<? extends K, ? extends V>- 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 is
nulland the comparator cannot handlenullkeys. UnsupportedOperationException- if adding to this map is not supported.
IllegalArgumentException- if a key or value cannot be added to this map.
remove
public V remove(Object key)Parameters
keyObject- the key of the mapping to remove.
Returns
null if 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 is
nulland the comparator cannot handlenullkeys.
size
public int size()Returns
values
public Collection<V> 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 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.
Returns
firstEntry
public Map.Entry<K, V> firstEntry()Returns
lastEntry
public Map.Entry<K, V> lastEntry()Returns
pollFirstEntry
public Map.Entry<K, V> pollFirstEntry()Returns
pollLastEntry
public Map.Entry<K, V> pollLastEntry()Returns
higherEntry
public Map.Entry<K, V> higherEntry(K key)Parameters
keyK- the key
Returns
Throws
ClassCastException- if the key cannot be compared with the keys in the map
NullPointerException- if the key is null and the map can not contain null key
higherKey
public K higherKey(K key)Parameters
keyK- the key
Returns
Throws
ClassCastException- if the key cannot be compared with the keys in the map
NullPointerException- if the key is null and the map can not contain null key
lowerEntry
public Map.Entry<K, V> lowerEntry(K key)Parameters
keyK- the key
Returns
Throws
ClassCastException- if the key cannot be compared with the keys in the map
NullPointerException- if the key is null and the map can not contain null key
lowerKey
public K lowerKey(K key)Parameters
keyK- the key
Returns
Throws
ClassCastException- if the key cannot be compared with the keys in the map
NullPointerException- if the key is null and the map can not contain null key
ceilingEntry
public Map.Entry<K, V> ceilingEntry(K key)Parameters
keyK- the key
Returns
Throws
ClassCastException- if the key cannot be compared with the keys in the map
NullPointerException- if the key is null and the map can not contain null key
ceilingKey
public K ceilingKey(K key)Parameters
keyK- the key
Returns
Throws
ClassCastException- if the key cannot be compared with the keys in the map
NullPointerException- if the key is null and the map can not contain null key
floorEntry
public Map.Entry<K, V> floorEntry(K key)Parameters
keyK- the key
Returns
Throws
ClassCastException- if the key cannot be compared with the keys in the map
NullPointerException- if the key is null and the map can not contain null key
floorKey
public K floorKey(K key)Parameters
keyK- the key
Returns
Throws
ClassCastException- if the key cannot be compared with the keys in the map
NullPointerException- if the key is null and the map can not contain null key
entrySet
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.Returns
descendingKeySet
public NavigableSet<K> descendingKeySet()Returns
descendingMap
public NavigableMap<K, V> descendingMap()Returns
subMap
public NavigableMap<K, V> subMap(K start, boolean startInclusive, K end, boolean endInclusive)Parameters
startK- the start key
startInclusiveboolean- true if the start key is in the returned map
endK- the end key
endInclusiveboolean- true if the end key is in the returned map
Returns
Throws
ClassCastException- when the class of the start or end key is inappropriate for this SubMap
NullPointerException- when the start or end key is null and this SortedMap does not support null keys
IllegalArgumentException- when the start key is greater than the end key
headMap
public NavigableMap<K, V> headMap(K end, boolean inclusive)Parameters
endK- the end key
inclusiveboolean- true if the end key is in the returned map
Returns
Throws
ClassCastException- when the class of the end key is inappropriate for this SubMap
NullPointerException- when the end key is null and this SortedMap does not support null keys
IllegalArgumentException- when the map is range-limited and end key is out of the range of the map
tailMap
public NavigableMap<K, V> tailMap(K start, boolean inclusive)Parameters
startK- the start key
inclusiveboolean- true if the start key is in the returned map
Returns
Throws
ClassCastException- when the class of the start key is inappropriate for this SubMap
NullPointerException- when the start key is null and this SortedMap does not support null keys
IllegalArgumentException- when the map is range-limited and start key is out of the range of the map
subMap
public 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. 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.
Parameters
startKeyK- the low boundary of the range (inclusive).
endKeyK- the high boundary of the range (exclusive),
Returns
Throws
ClassCastException- if the start or end key cannot be compared with the keys in this map.
NullPointerException- if the start or end key is
nulland 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
public 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. 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.
Parameters
endKeyK- the high boundary of the range specified.
Returns
endKey.Throws
ClassCastException- if the specified key cannot be compared with the keys in this map.
NullPointerException- if the specified key is
nulland 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
public 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. 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.
Parameters
startKeyK- the low boundary of the range specified.
Returns
startKey.Throws
ClassCastException- if the specified key cannot be compared with the keys in this map.
NullPointerException- if the specified key is
nulland 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.