java.util.TreeMap<K, V>
TreeMap is an implementation of SortedMap. All optional operations are
supported, adding and removing. 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.
Summary
Public Constructors
Public Methods
clear,
clone,
containsKey,
containsValue,
entrySet,
equals,
get,
hashCode,
isEmpty,
keySet,
put,
putAll,
remove,
size,
toString,
values
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Methods inherited
from interface
java.util.Map
clear,
containsKey,
containsValue,
entrySet,
equals,
get,
hashCode,
isEmpty,
keySet,
put,
putAll,
remove,
size,
values
Details
Public Constructors
public
TreeMap()
Constructs a new empty instance of TreeMap.
public
TreeMap(Comparator<? super K> comparator)
Constructs a new empty instance of TreeMap which uses the specified
Comparator.
Parameters
comparator
| the Comparator
|
public
TreeMap(Map<? extends K, ? extends V> map)
Constructs a new instance of TreeMap containing the mappings from the
specified Map and using the natural ordering.
Throws
ClassCastException
| when a key in the Map does not implement the Comparable
interface, or they keys in the Map cannot be compared
|
public
TreeMap(SortedMap<K, ? extends V> map)
Constructs a new instance of TreeMap containing the mappings from the
specified SortedMap and using the same Comparator.
Public Methods
public
void
clear()
Removes all mappings from this TreeMap, leaving it empty.
public
Object
clone()
Returns a new TreeMap with the same mappings, size and comparator as this
TreeMap.
Returns
- a shallow copy of this TreeMap
public
Comparator<? super K>
comparator()
Returns the Comparator used to compare elements in this TreeMap.
Returns
- a Comparator or null if the natural ordering is used
public
boolean
containsKey(Object key)
Searches this TreeMap for the specified key.
Parameters
key
| the object to search for |
Returns
- true if
key
is a key of this TreeMap, false
otherwise
public
boolean
containsValue(Object value)
Searches this TreeMap for the specified value.
Parameters
value
| the object to search for |
Returns
- true if
value
is a value of this TreeMap, false
otherwise
public
Set<Entry<K, V>>
entrySet()
Returns a Set of the mappings contained in this TreeMap. Each element in
the set is a Map.Entry. The set is backed by this TreeMap so changes to
one are reflected by the other. The set does not support adding.
public
K
firstKey()
Answer the first sorted key in this TreeMap.
public
V
get(Object key)
Returns the value of the mapping with the specified key.
Returns
- the value of the mapping with the specified key
public
SortedMap<K, V>
headMap(K endKey)
Returns a SortedMap of the specified portion of this TreeMap which
contains keys less than the end key. The returned SortedMap is backed by
this TreeMap so changes to one are reflected by the other.
Returns
- a sub-map where the keys are less than
endKey
public
Set<K>
keySet()
Returns 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.
public
K
lastKey()
Answer the last sorted key in this TreeMap.
public
V
put(K key, V value)
Maps the specified key to the specified value.
Parameters
key
| the key |
value
| the value |
Returns
- the value of any previous mapping with the specified key or null
if there was no mapping
public
void
putAll(Map<? extends K, ? extends V> map)
Copies every mapping in the specified Map to this TreeMap.
Parameters
map
| the Map to copy mappings from |
public
V
remove(Object key)
Removes a mapping with the specified key from this TreeMap.
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
public
int
size()
Returns the number of mappings in this TreeMap.
Returns
- the number of mappings in this TreeMap
public
SortedMap<K, V>
subMap(K startKey, K endKey)
Returns a SortedMap of the specified portion of this TreeMap which
contains keys greater or equal to the start key but less than the end
key. The returned SortedMap is backed by this TreeMap so changes to one
are reflected by the other.
Parameters
startKey
| the start key |
endKey
| the end key |
Returns
- a sub-map where the keys are greater or equal to
startKey
and less than endKey
public
SortedMap<K, V>
tailMap(K startKey)
Returns a SortedMap of the specified portion of this TreeMap which
contains keys greater or equal to the start key. The returned SortedMap
is backed by this TreeMap so changes to one are reflected by the other.
Returns
- a sub-map where the keys are greater or equal to
startKey
Returns a Collection of the values contained in this TreeMap. The
collection is backed by this TreeMap so changes to one are reflected by
the other. The collection does not support adding.
Returns
- a Collection of the values