Class DoubleOrderedMap
- java.lang.Object
-
- java.util.AbstractMap
-
- org.apache.commons.collections.DoubleOrderedMap
-
- All Implemented Interfaces:
java.util.Map
@Deprecated(since="2021-04-30") public final class DoubleOrderedMap extends java.util.AbstractMap
Deprecated.Replaced by TreeBidiMap in bidimap subpackage. Due to be removed in v4.0.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. (See also the new
DualTreeBidiMap
andDualHashBidiMap
implementations.)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.
- Since:
- Commons Collections 2.0
- See Also:
BidiMap
,DualTreeBidiMap
,DualHashBidiMap
-
-
Constructor Summary
Constructors Constructor Description DoubleOrderedMap()
Deprecated.Construct a new DoubleOrderedMapDoubleOrderedMap(java.util.Map map)
Deprecated.Constructs a new DoubleOrderedMap from an existing Map, with keys and values sorted
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
clear()
Deprecated.Removes all mappings from this mapboolean
containsKey(java.lang.Object key)
Deprecated.Returns true if this map contains a mapping for the specified key.boolean
containsValue(java.lang.Object value)
Deprecated.Returns true if this map maps one or more keys to the specified value.java.util.Set
entrySet()
Deprecated.Returns a set view of the mappings contained in this map.java.util.Set
entrySetByValue()
Deprecated.Returns a set view of the mappings contained in this map.java.lang.Object
get(java.lang.Object key)
Deprecated.Returns the value to which this map maps the specified key.java.lang.Object
getKeyForValue(java.lang.Object value)
Deprecated.Returns the key to which this map maps the specified value.java.util.Set
keySet()
Deprecated.Returns a set view of the keys contained in this map.java.util.Set
keySetByValue()
Deprecated.Returns a set view of the keys contained in this map.java.lang.Object
put(java.lang.Object key, java.lang.Object value)
Deprecated.Associates the specified value with the specified key in this map.java.lang.Object
remove(java.lang.Object key)
Deprecated.Removes the mapping for this key from this map if presentjava.lang.Object
removeValue(java.lang.Object value)
Deprecated.Removes the mapping for this value from this map if presentint
size()
Deprecated.Returns the number of key-value mappings in this map.java.util.Collection
values()
Deprecated.Returns a collection view of the values contained in this map.java.util.Collection
valuesByValue()
Deprecated.Returns a collection view of the values contained in this map.
-
-
-
Constructor Detail
-
DoubleOrderedMap
public DoubleOrderedMap()
Deprecated.Construct a new DoubleOrderedMap
-
DoubleOrderedMap
public DoubleOrderedMap(java.util.Map map) throws java.lang.ClassCastException, java.lang.NullPointerException, java.lang.IllegalArgumentException
Deprecated.Constructs a new DoubleOrderedMap from an existing Map, with keys and values sorted- Parameters:
map
- the map whose mappings are to be placed in this map.- Throws:
java.lang.ClassCastException
- if the keys in the map are not Comparable, or are not mutually comparable; also if the values in the map are not Comparable, or are not mutually Comparablejava.lang.NullPointerException
- if any key or value in the map is nulljava.lang.IllegalArgumentException
- if there are duplicate keys or duplicate values in the map
-
-
Method Detail
-
getKeyForValue
public java.lang.Object getKeyForValue(java.lang.Object value) throws java.lang.ClassCastException, java.lang.NullPointerException
Deprecated.Returns the key to which this map maps the specified value. Returns null if the map contains no mapping for this value.- Parameters:
value
- value whose associated key is to be returned.- Returns:
- the key to which this map maps the specified value, or null if the map contains no mapping for this value.
- Throws:
java.lang.ClassCastException
- if the value is of an inappropriate type for this map.java.lang.NullPointerException
- if the value is null
-
removeValue
public java.lang.Object removeValue(java.lang.Object value)
Deprecated.Removes the mapping for this value from this map if present- Parameters:
value
- value whose mapping is to be removed from the map.- Returns:
- previous key associated with specified value, or null if there was no mapping for value.
-
entrySetByValue
public java.util.Set entrySetByValue()
Deprecated.Returns a set view of the mappings contained in this map. Each element in the returned set is a Map.Entry. 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, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.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.
- Returns:
- a set view of the mappings contained in this map.
-
keySetByValue
public java.util.Set keySetByValue()
Deprecated.Returns a set view of the keys contained in this map. 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, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.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.
- Returns:
- a set view of the keys contained in this map.
-
valuesByValue
public java.util.Collection valuesByValue()
Deprecated.Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.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.
- Returns:
- a collection view of the values contained in this map.
-
size
public int size()
Deprecated.Returns the number of key-value mappings in this map. If the map contains more than Integer.MAXVALUE elements, returns Integer.MAXVALUE.- Specified by:
size
in interfacejava.util.Map
- Overrides:
size
in classjava.util.AbstractMap
- Returns:
- the number of key-value mappings in this map.
-
containsKey
public boolean containsKey(java.lang.Object key) throws java.lang.ClassCastException, java.lang.NullPointerException
Deprecated.Returns true if this map contains a mapping for the specified key.- Specified by:
containsKey
in interfacejava.util.Map
- Overrides:
containsKey
in classjava.util.AbstractMap
- Parameters:
key
- key whose presence in this map is to be tested.- Returns:
- true if this map contains a mapping for the specified key.
- Throws:
java.lang.ClassCastException
- if the key is of an inappropriate type for this map.java.lang.NullPointerException
- if the key is null
-
containsValue
public boolean containsValue(java.lang.Object value)
Deprecated.Returns true if this map maps one or more keys to the specified value.- Specified by:
containsValue
in interfacejava.util.Map
- Overrides:
containsValue
in classjava.util.AbstractMap
- Parameters:
value
- value whose presence in this map is to be tested.- Returns:
- true if this map maps one or more keys to the specified value.
-
get
public java.lang.Object get(java.lang.Object key) throws java.lang.ClassCastException, java.lang.NullPointerException
Deprecated.Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key.- Specified by:
get
in interfacejava.util.Map
- Overrides:
get
in classjava.util.AbstractMap
- Parameters:
key
- key whose associated value is to be returned.- Returns:
- the value to which this map maps the specified key, or null if the map contains no mapping for this key.
- Throws:
java.lang.ClassCastException
- if the key is of an inappropriate type for this map.java.lang.NullPointerException
- if the key is null
-
put
public java.lang.Object put(java.lang.Object key, java.lang.Object value) throws java.lang.ClassCastException, java.lang.NullPointerException, java.lang.IllegalArgumentException
Deprecated.Associates the specified value with the specified key in this map.- Specified by:
put
in interfacejava.util.Map
- Overrides:
put
in classjava.util.AbstractMap
- Parameters:
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.- Returns:
- null
- Throws:
java.lang.ClassCastException
- if the class of the specified key or value prevents it from being stored in this map.java.lang.NullPointerException
- if the specified key or value is nulljava.lang.IllegalArgumentException
- if the key duplicates an existing key, or if the value duplicates an existing value
-
remove
public java.lang.Object remove(java.lang.Object key)
Deprecated.Removes the mapping for this key from this map if present- Specified by:
remove
in interfacejava.util.Map
- Overrides:
remove
in classjava.util.AbstractMap
- Parameters:
key
- key whose mapping is to be removed from the map.- Returns:
- previous value associated with specified key, or null if there was no mapping for key.
-
clear
public void clear()
Deprecated.Removes all mappings from this map- Specified by:
clear
in interfacejava.util.Map
- Overrides:
clear
in classjava.util.AbstractMap
-
keySet
public java.util.Set keySet()
Deprecated.Returns a set view of the keys contained in this map. 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, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.- Specified by:
keySet
in interfacejava.util.Map
- Overrides:
keySet
in classjava.util.AbstractMap
- Returns:
- a set view of the keys contained in this map.
-
values
public java.util.Collection values()
Deprecated.Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.- Specified by:
values
in interfacejava.util.Map
- Overrides:
values
in classjava.util.AbstractMap
- Returns:
- a collection view of the values contained in this map.
-
entrySet
public java.util.Set entrySet()
Deprecated.Returns a set view of the mappings contained in this map. Each element in the returned set is a Map.Entry. 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, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations. The setValue method is not supported on the Map Entry.
- Specified by:
entrySet
in interfacejava.util.Map
- Specified by:
entrySet
in classjava.util.AbstractMap
- Returns:
- a set view of the mappings contained in this map.
-
-