public class TreeBidiMap<K extends Comparable<K> extends Object,V extends Comparable <V>> implements OrderedBidiMap <K ,V>, Serializable
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 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.
| Constructor and Description |
|---|
TreeBidiMap()
Constructs a new empty TreeBidiMap.
|
TreeBidiMap(Map
Constructs a new TreeBidiMap by copying an existing Map.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all mappings from this map.
|
boolean |
containsKey(Object
Checks whether this map contains the a mapping for the specified key.
|
boolean |
containsValue(Object
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
Compares for equals as per the API.
|
K |
firstKey()
Gets the first (lowest) key currently in this map.
|
V |
get(Object
Gets the value to which this map maps the specified key.
|
K |
getKey(Object
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.
|
OrderedBidiMap |
inverseBidiMap()
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.
|
K |
lastKey()
Gets the last (highest) key currently in this map.
|
OrderedMapIterator |
mapIterator()
Obtains a
MapIterator over the map.
|
K |
nextKey(K key)
Gets the next key after the one specified.
|
K |
previousKey(K key)
Gets the previous key before the one specified.
|
V |
put(K key, V value)
Puts the key-value pair into the map, replacing any previous pair.
|
void |
putAll(Map
Puts all the mappings from the specified map into this map.
|
V |
remove(Object
Removes the mapping for this key from this map if present.
|
K |
removeValue(Object
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.
|
Set |
values()
Returns a set view of the values contained in this map in key order.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllpublic TreeBidiMap()
public TreeBidiMap(Map<? extends K ,? extends V> map)
map - the map to copy
ClassCastException - if the keys/values in the map are not Comparable or are not mutually comparable
NullPointerException - if any key or value in the map is null
public int size()
size in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
size in interface
Get<K extends Comparable<K>,V extends Comparable<V>>
Map.size()
public boolean isEmpty()
isEmpty in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
isEmpty in interface
Get<K extends Comparable<K>,V extends Comparable<V>>
Map.isEmpty()
public boolean containsKey(Objectkey)
The key must implement Comparable.
containsKey in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
containsKey in interface
Get<K extends Comparable<K>,V extends Comparable<V>>
key - key whose presence in this map is to be tested
ClassCastException - if the key is of an inappropriate type
NullPointerException - if the key is null
Map.containsKey(Object)
public boolean containsValue(Objectvalue)
The value must implement Comparable.
containsValue in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
containsValue in interface
Get<K extends Comparable<K>,V extends Comparable<V>>
value - value whose presence in this map is to be tested
ClassCastException - if the value is of an inappropriate type
NullPointerException - if the value is null
Map.containsValue(Object)
public V get(Objectkey)
The key must implement Comparable.
get in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
get in interface
Get<K extends Comparable<K>,V extends Comparable<V>>
key - key whose associated value is to be returned
ClassCastException - if the key is of an inappropriate type
NullPointerException - if the key is null
Map.get(Object)
public V put(K key, V value)
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
Map<K extends Comparable<K>,V extends Comparable<V>>
put in interface
BidiMap<K extends Comparable<K>,V extends Comparable<V>>
put in interface
Put<K extends Comparable<K>,V extends Comparable<V>>
key - key with which the specified value is to be associated
value - value to be associated with the specified key
ClassCastException - if the key is of an inappropriate type
NullPointerException - if the key is null
Map.put(Object, Object)
public void putAll(Map<? extends K ,? extends V> map)
All keys and values must implement Comparable.
putAll in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
putAll in interface
Put<K extends Comparable<K>,V extends Comparable<V>>
map - the map to copy from
Map.putAll(Map)
public V remove(Objectkey)
The key must implement Comparable.
remove in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
remove in interface
Get<K extends Comparable<K>,V extends Comparable<V>>
key - key whose mapping is to be removed from the map.
ClassCastException - if the key is of an inappropriate type
NullPointerException - if the key is null
Map.remove(Object)
public void clear()
clear in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
clear in interface
Put<K extends Comparable<K>,V extends Comparable<V>>
Map.clear()
public K getKey(Objectvalue)
The value must implement Comparable.
getKey in interface
BidiMap<K extends Comparable<K>,V extends Comparable<V>>
value - value whose associated key is to be returned.
ClassCastException - if the value is of an inappropriate type
NullPointerException - if the value is null
public K removeValue(Objectvalue)
The value must implement Comparable.
removeValue in interface
BidiMap<K extends Comparable<K>,V extends Comparable<V>>
value - value whose mapping is to be removed from the map
ClassCastException - if the value is of an inappropriate type
NullPointerException - if the value is null
public K firstKey()
firstKey in interface
OrderedMap<K extends Comparable<K>,V extends Comparable<V>>
NoSuchElementException - if this map is empty
public K lastKey()
lastKey in interface
OrderedMap<K extends Comparable<K>,V extends Comparable<V>>
NoSuchElementException - if this map is empty
public K nextKey(K key)
The key must implement Comparable.
nextKey in interface
OrderedMap<K extends Comparable<K>,V extends Comparable<V>>
key - the key to search for next from
public K previousKey(K key)
The key must implement Comparable.
previousKey in interface
OrderedMap<K extends Comparable<K>,V extends Comparable<V>>
key - the key to search for previous from
public Set<K> keySet()
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.
keySet in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
keySet in interface
Get<K extends Comparable<K>,V extends Comparable<V>>
Map.keySet()
public Set<V> values()
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.
values in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
values in interface
BidiMap<K extends Comparable<K>,V extends Comparable<V>>
values in interface
Get<K extends Comparable<K>,V extends Comparable<V>>
Map.values()
public Set<Map .Entry <K ,V>> entrySet()
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.
entrySet in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
entrySet in interface
Get<K extends Comparable<K>,V extends Comparable<V>>
Map.entrySet()
public OrderedMapIterator<K ,V> mapIterator()
IterableGet
MapIterator over the map.
A map iterator is an efficient way of iterating over maps. There is no need to access the entry set or use Map Entry objects.
IterableMap
map = new HashedMap
(); MapIterator
it = map.mapIterator(); while (it.hasNext()) { String key = it.next(); Integer value = it.getValue(); it.setValue(value + 1); }
mapIterator in interface
IterableGet<K extends Comparable<K>,V extends Comparable<V>>
mapIterator in interface
OrderedMap<K extends Comparable<K>,V extends Comparable<V>>
public OrderedBidiMap<V ,K> inverseBidiMap()
inverseBidiMap in interface
BidiMap<K extends Comparable<K>,V extends Comparable<V>>
inverseBidiMap in interface
OrderedBidiMap<K extends Comparable<K>,V extends Comparable<V>>
public boolean equals(Objectobj)
equals in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
equals in class
Object
obj - the object to compare to
public int hashCode()
hashCode in interface
Map<K extends Comparable<K>,V extends Comparable<V>>
hashCode in class
Object