org.apache.commons.collections.bidimap
Class TreeBidiMap
java.lang.Object
org.apache.commons.collections.bidimap.TreeBidiMap
- BidiMap, IterableMap, Map, OrderedBidiMap, OrderedMap
public class TreeBidiMap
extends java.lang.Object
Red-Black tree-based implementation of BidiMap where all objects added
implement the
Comparable
interface.
This class guarantees that the map will be in both ascending key order
and ascending value order, sorted according to the natural order for
the key's and value's classes.
This Map is intended for applications that need to be able to look
up a key-value pairing by either key or value, and need to do so
with equal efficiency.
While that goal could be accomplished by taking a pair of TreeMaps
and redirecting requests to the appropriate TreeMap (e.g.,
containsKey would be directed to the TreeMap that maps values to
keys, containsValue would be directed to the TreeMap that maps keys
to values), there are problems with that implementation.
If the data contained in the TreeMaps is large, the cost of redundant
storage becomes significant. The
DualTreeBidiMap
and
DualHashBidiMap
implementations use this approach.
This solution keeps minimizes the data storage by holding data only once.
The red-black algorithm is based on java util TreeMap, but has been modified
to simultaneously map a tree node by key and by value. This doubles the
cost of put operations (but so does using two TreeMaps), and nearly doubles
the cost of remove operations (there is a savings in that the lookup of the
node to be removed only has to be performed once). And since only one node
contains the key and value, storage is significantly less than that
required by two TreeMaps.
The Map.Entry instances returned by the appropriate methods will
not allow setValue() and will throw an
UnsupportedOperationException on attempts to call that method.
$Revision: 1.14 $ $Date: 2004/05/26 21:58:02 $- Marc Johnson
- Stephen Colebourne
- Commons Collections 3.0 (previously DoubleOrderedMap v2.0)
TreeBidiMap() - Constructs a new empty TreeBidiMap.
|
TreeBidiMap(Map map) - Constructs a new TreeBidiMap by copying an existing Map.
|
void | clear() - Removes all mappings from this map.
|
boolean | containsKey(Object key) - Checks whether this map contains the a mapping for the specified key.
|
boolean | containsValue(Object value) - Checks whether this map contains the a mapping for the specified value.
|
Set | entrySet() - Returns a set view of the entries contained in this map in key order.
|
boolean | equals(Object obj) - Compares for equals as per the API.
|
Object | firstKey() - Gets the first (lowest) key currently in this map.
|
Object | get(Object key) - Gets the value to which this map maps the specified key.
|
Object | getKey(Object value) - Returns the key to which this map maps the specified value.
|
int | hashCode() - Gets the hash code value for this map as per the API.
|
BidiMap | inverseBidiMap() - Gets the inverse map for comparison.
|
OrderedBidiMap | inverseOrderedBidiMap() - Gets the inverse map for comparison.
|
boolean | isEmpty() - Checks whether the map is empty or not.
|
Set | keySet() - Returns a set view of the keys contained in this map in key order.
|
Object | lastKey() - Gets the last (highest) key currently in this map.
|
MapIterator | mapIterator() - Gets an iterator over the map entries.
|
Object | nextKey(Object key) - Gets the next key after the one specified.
|
OrderedMapIterator | orderedMapIterator() - Gets an ordered iterator over the map entries.
|
Object | previousKey(Object key) - Gets the previous key before the one specified.
|
Object | put(Object key, Object value) - Puts the key-value pair into the map, replacing any previous pair.
|
void | putAll(Map map) - Puts all the mappings from the specified map into this map.
|
Object | remove(Object key) - Removes the mapping for this key from this map if present.
|
Object | removeValue(Object value) - Removes the mapping for this value from this map if present.
|
int | size() - Returns the number of key-value mappings in this map.
|
String | toString() - Returns a string version of this Map in standard format.
|
Collection | values() - Returns a set view of the values contained in this map in key order.
|
TreeBidiMap
public TreeBidiMap()
Constructs a new empty TreeBidiMap.
TreeBidiMap
public TreeBidiMap(Map map)
Constructs a new TreeBidiMap by copying an existing Map.
clear
public void clear()
Removes all mappings from this map.
containsKey
public boolean containsKey(Object key)
Checks whether this map contains the a mapping for the specified key.
The key must implement
Comparable
.
key
- key whose presence in this map is to be tested
- true if this map contains a mapping for the specified key
containsValue
public boolean containsValue(Object value)
Checks whether this map contains the a mapping for the specified value.
The value must implement
Comparable
.
value
- value whose presence in this map is to be tested
- true if this map contains a mapping for the specified value
entrySet
public Set entrySet()
Returns a set view of the entries contained in this map in key order.
For simple iteration through the map, the MapIterator is quicker.
The set is backed by the map, so changes to the map are reflected in
the set, and vice-versa. If the map is modified while an iteration over
the set is in progress, the results of the iteration are undefined.
The set supports element removal, which removes the corresponding mapping
from the map. It does not support the add or addAll operations.
The returned MapEntry objects do not support setValue.
- a set view of the values contained in this map.
equals
public boolean equals(Object obj)
Compares for equals as per the API.
obj
- the object to compare to
firstKey
public Object firstKey()
Gets the first (lowest) key currently in this map.
- firstKey in interface OrderedMap
- the first (lowest) key currently in this sorted map
get
public Object get(Object key)
Gets the value to which this map maps the specified key.
Returns null if the map contains no mapping for this key.
The key must implement
Comparable
.
key
- key whose associated value is to be returned
- the value to which this map maps the specified key,
or null if the map contains no mapping for this key
getKey
public Object getKey(Object value)
Returns the key to which this map maps the specified value.
Returns null if the map contains no mapping for this value.
The value must implement
Comparable
.
- getKey in interface BidiMap
value
- value whose associated key is to be returned.
- the key to which this map maps the specified value,
or null if the map contains no mapping for this value.
hashCode
public int hashCode()
Gets the hash code value for this map as per the API.
- the hash code value for this map
isEmpty
public boolean isEmpty()
Checks whether the map is empty or not.
keySet
public Set keySet()
Returns a set view of the keys contained in this map in key order.
The set is backed by the map, so changes to the map are reflected in
the set, and vice-versa. If the map is modified while an iteration over
the set is in progress, the results of the iteration are undefined.
The set supports element removal, which removes the corresponding mapping
from the map. It does not support the add or addAll operations.
- a set view of the keys contained in this map.
lastKey
public Object lastKey()
Gets the last (highest) key currently in this map.
- lastKey in interface OrderedMap
- the last (highest) key currently in this sorted map
nextKey
public Object nextKey(Object key)
Gets the next key after the one specified.
The key must implement
Comparable
.
- nextKey in interface OrderedMap
key
- the key to search for next from
- the next key, null if no match or at end
previousKey
public Object previousKey(Object key)
Gets the previous key before the one specified.
The key must implement
Comparable
.
- previousKey in interface OrderedMap
key
- the key to search for previous from
- the previous key, null if no match or at start
put
public Object put(Object key,
Object value)
Puts the key-value pair into the map, replacing any previous pair.
When adding a key-value pair, the value may already exist in the map
against a different key. That mapping is removed, to ensure that the
value only occurs once in the inverse map.
BidiMap map1 = new TreeBidiMap();
map.put("A","B"); // contains A mapped to B, as per Map
map.put("A","C"); // contains A mapped to C, as per Map
BidiMap map2 = new TreeBidiMap();
map.put("A","B"); // contains A mapped to B, as per Map
map.put("C","B"); // contains C mapped to B, key A is removed
Both key and value must implement
Comparable
.
- put in interface BidiMap
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key
- the previous value for the key
putAll
public void putAll(Map map)
Puts all the mappings from the specified map into this map.
All keys and values must implement
Comparable
.
map
- the map to copy from
remove
public Object remove(Object key)
Removes the mapping for this key from this map if present.
The key must implement
Comparable
.
key
- key whose mapping is to be removed from the map.
- previous value associated with specified key,
or null if there was no mapping for key.
removeValue
public Object removeValue(Object value)
Removes the mapping for this value from this map if present.
The value must implement
Comparable
.
- removeValue in interface BidiMap
value
- value whose mapping is to be removed from the map
- previous key associated with specified value,
or null if there was no mapping for value.
size
public int size()
Returns the number of key-value mappings in this map.
- the number of key-value mappings in this map
toString
public String toString()
Returns a string version of this Map in standard format.
- a standard format string version of the map
values
public Collection values()
Returns a set view of the values contained in this map in key order.
The returned object can be cast to a Set.
The set is backed by the map, so changes to the map are reflected in
the set, and vice-versa. If the map is modified while an iteration over
the set is in progress, the results of the iteration are undefined.
The set supports element removal, which removes the corresponding mapping
from the map. It does not support the add or addAll operations.
- a set view of the values contained in this map.
Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.