|
JavaTM 2 Platform Standard Edition |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.util.AbstractMap | +--java.util.TreeMap
Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Corman, Leiserson, and Rivest's Introduction to Algorithms.
Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.
Note that this implementation is not synchronized. If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with an existing key is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
Map m = Collections.synchronizedMap(new TreeMap(...));
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator throws a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Map
,
HashMap
,
Hashtable
,
Comparable
,
Comparator
,
Collection
,
Collections.synchronizedMap(Map)
, Serialized FormInner classes inherited from class java.util.Map |
Map.Entry |
Constructor Summary | |
TreeMap()
Constructs a new, empty map, sorted according to the keys' natural order. |
|
TreeMap(Comparator c)
Constructs a new, empty map, sorted according to the given comparator. |
|
TreeMap(Map m)
Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order. |
|
TreeMap(SortedMap m)
Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering. |
Method Summary | |
void |
clear()
Removes all mappings from this TreeMap. |
Object |
clone()
Returns a shallow copy of this TreeMap instance. |
Comparator |
comparator()
Returns the comparator used to order this map, or null if this map uses its keys' natural order. |
boolean |
containsKey(Object key)
Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue(Object value)
Returns true if this map maps one or more keys to the specified value. |
Set |
entrySet()
Returns a set view of the mappings contained in this map. |
Object |
firstKey()
Returns the first (lowest) key currently in this sorted map. |
Object |
get(Object key)
Returns the value to which this map maps the specified key. |
SortedMap |
headMap(Object toKey)
Returns a view of the portion of this map whose keys are strictly less than toKey. |
Set |
keySet()
Returns a Set view of the keys contained in this map. |
Object |
lastKey()
Returns the last (highest) key currently in this sorted map. |
Object |
put(Object key,
Object value)
Associates the specified value with the specified key in this map. |
void |
putAll(Map map)
Copies all of the mappings from the specified map to this map. |
Object |
remove(Object key)
Removes the mapping for this key from this TreeMap if present. |
int |
size()
Returns the number of key-value mappings in this map. |
SortedMap |
subMap(Object fromKey,
Object toKey)
Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. |
SortedMap |
tailMap(Object fromKey)
Returns a view of the portion of this map whose keys are greater than or equal to fromKey. |
Collection |
values()
Returns a collection view of the values contained in this map. |
Methods inherited from class java.util.AbstractMap |
equals,
hashCode,
isEmpty,
toString |
Methods inherited from class java.lang.Object |
finalize,
getClass,
notify,
notifyAll,
wait,
wait,
wait |
Constructor Detail |
public TreeMap()
Comparable
public TreeMap(Comparator c)
public TreeMap(Map m)
public TreeMap(SortedMap m)
Method Detail |
public int size()
public boolean containsKey(Object key)
key
- key whose presence in this map is to be tested.public boolean containsValue(Object value)
value
- value whose presence in this Map is to be tested.public Object get(Object key)
key
- key whose associated value is to be returned.containsKey(Object)
public Comparator comparator()
public Object firstKey()
public Object lastKey()
public void putAll(Map map)
t
- Mappings to be stored in this map.public Object put(Object key, Object value)
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.public Object remove(Object key)
public void clear()
public Object clone()
public Set keySet()
public Collection values()
public Set entrySet()
Map.Entry
public SortedMap subMap(Object fromKey, Object toKey)
The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key less than fromKey or greater than or equal to toKey.
Note: this method always returns a half-open range (which includes its low endpoint but not its high endpoint). If you need a closed range (which includes both endpoints), and the key type allows for calculation of the successor a given key, merely request the subrange from lowEndpoint to successor(highEndpoint). For example, suppose that m is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, inclusive:
SortedMap sub = m.submap(low, high+"\0");A similar technique can be used to generate an open range (which contains neither endpoint). The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, exclusive:
SortedMap sub = m.subMap(low+"\0", high);
fromKey
- low endpoint (inclusive) of the subMap.toKey
- high endpoint (exclusive) of the subMap.public SortedMap headMap(Object toKey)
The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key greater than or equal to toKey.
Note: this method always returns a view that does not contain its (high) endpoint. If you need a view that does contain this endpoint, and the key type allows for calculation of the successor a given key, merely request a headMap bounded by successor(highEndpoint). For example, suppose that suppose that m is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are less than or equal to high:
SortedMap head = m.headMap(high+"\0");
toKey
- high endpoint (exclusive) of the headMap.public SortedMap tailMap(Object fromKey)
The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key less than fromKey.
Note: this method always returns a view that contains its (low) endpoint. If you need a view that does not contain this endpoint, and the element type allows for calculation of the successor a given value, merely request a tailMap bounded by successor(lowEndpoint). For For example, suppose that suppose that m is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are strictly greater than low:
SortedMap tail = m.tailMap(low+"\0");
fromKey
- low endpoint (inclusive) of the tailMap.
|
JavaTM 2 Platform Standard Edition |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |