|
||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.util.AbstractMap | +--org.apache.commons.collections.DoubleOrderedMap
Red-Black tree-based implementation of Map. 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, particularly when trying to keep the two TreeMaps synchronized with each other. And if the data contained in the TreeMaps is large, the cost of redundant storage becomes significant.
This solution keeps the data properly synchronized and minimizes the data storage. The red-black algorithm is based on TreeMap's, 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.
There are some limitations placed on data kept in this Map. The biggest one is this:
When performing a put operation, neither the key nor the value may already exist in the Map. In the java.util Map implementations (HashMap, TreeMap), you can perform a put with an already mapped key, and neither cares about duplicate values at all ... but this implementation's put method with throw an IllegalArgumentException if either the key or the value is already in the Map.
Obviously, that same restriction (and consequence of failing to heed that restriction) applies to the putAll method.
The Map.Entry instances returned by the appropriate methods will not allow setValue() and will throw an UnsupportedOperationException on attempts to call that method.
New methods are added to take advantage of the fact that values are kept sorted independently of their keys:
Object getKeyForValue(Object value) is the opposite of get; it takes a value and returns its key, if any.
Object removeValue(Object value) finds and removes the specified value and returns the now un-used key.
Set entrySetByValue() returns the Map.Entry's in a Set whose iterator will iterate over the Map.Entry's in ascending order by their corresponding values.
Set keySetByValue() returns the keys in a Set whose iterator will iterate over the keys in ascending order by their corresponding values.
Collection valuesByValue() returns the values in a Collection whose iterator will iterate over the values in ascending order.
Constructor Summary | |
DoubleOrderedMap()
Construct a new DoubleOrderedMap |
|
DoubleOrderedMap(Map map)
Constructs a new DoubleOrderedMap from an existing Map, with keys and values sorted |
Method Summary | |
void |
clear()
Removes all mappings from this map |
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. |
Set |
entrySetByValue()
Returns a set view of the mappings contained in this map. |
Object |
get(Object key)
Returns the value to which this map maps the specified key. |
Object |
getKeyForValue(Object value)
Returns the key to which this map maps the specified value. |
Set |
keySet()
Returns a set view of the keys contained in this map. |
Set |
keySetByValue()
Returns a set view of the keys contained in this map. |
Object |
put(Object key,
Object value)
Associates the specified value with the specified key in 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. |
Collection |
values()
Returns a collection view of the values contained in this map. |
Collection |
valuesByValue()
Returns a collection view of the values contained in this map. |
Methods inherited from class java.util.AbstractMap |
equals,
hashCode,
isEmpty,
putAll,
toString |
Methods inherited from class java.lang.Object |
clone,
finalize,
getClass,
notify,
notifyAll,
wait,
wait,
wait |
Constructor Detail |
public DoubleOrderedMap()
public DoubleOrderedMap(Map map) throws ClassCastException, NullPointerException, IllegalArgumentException
map
- the map whose mappings are to be placed in this map.Method Detail |
public Object getKeyForValue(Object value) throws ClassCastException, NullPointerException
value
- value whose associated key is to be returned.public Object removeValue(Object value)
value
- value whose mapping is to be removed from the map.public Set entrySetByValue()
The difference between this method and entrySet is that entrySet's iterator() method returns an iterator that iterates over the mappings in ascending order by key. This method's iterator method iterates over the mappings in ascending order by value.
public Set keySetByValue()
The difference between this method and keySet is that keySet's iterator() method returns an iterator that iterates over the keys in ascending order by key. This method's iterator method iterates over the keys in ascending order by value.
public Collection valuesByValue()
The difference between this method and values is that values's iterator() method returns an iterator that iterates over the values in ascending order by key. This method's iterator method iterates over the values in ascending order by key.
public int size()
public boolean containsKey(Object key) throws ClassCastException, NullPointerException
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) throws ClassCastException, NullPointerException
key
- key whose associated value is to be returned.public Object put(Object key, Object value) throws ClassCastException, NullPointerException, IllegalArgumentException
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)
key
- key whose mapping is to be removed from the map.public void clear()
public Set keySet()
public Collection values()
public Set entrySet()
|
||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |