|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.collections.bidimap.TreeBidiMap<K,V>
public class TreeBidiMap<K extends Comparable,V extends Comparable>
Red-Black tree-based implementation of BidiMap where all objects added
implement the Comparable
interface.
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.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Constructor Summary | |
---|---|
TreeBidiMap()
Constructs a new empty TreeBidiMap. |
|
TreeBidiMap(Map map)
Constructs a new TreeBidiMap by copying an existing Map. |
Method Summary | |
---|---|
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<Map.Entry<K,V>> |
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. |
K |
firstKey()
Gets the first (lowest) key currently in this map. |
V |
get(Object key)
Gets the value to which this map maps the specified key. |
K |
getKey(V 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<V,K> |
inverseBidiMap()
Gets the inverse map for comparison. |
OrderedBidiMap<V,K> |
inverseOrderedBidiMap()
Gets the inverse map for comparison. |
boolean |
isEmpty()
Checks whether the map is empty or not. |
Set<K> |
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. |
MapIterator<K,V> |
mapIterator()
Gets an iterator over the map entries. |
K |
nextKey(K key)
Gets the next key after the one specified. |
OrderedMapIterator<K,V> |
orderedMapIterator()
Gets an ordered iterator over the map entries. |
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<? extends K,? extends V> map)
Puts all the mappings from the specified map into this map. |
V |
remove(Object key)
Removes the mapping for this key from this map if present. |
K |
removeValue(V 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<V> |
values()
Returns a set view of the values contained in this map in key order. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public TreeBidiMap()
public TreeBidiMap(Map 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 nullMethod Detail |
---|
public int size()
size
in interface Map<K extends Comparable,V extends Comparable>
public boolean isEmpty()
isEmpty
in interface Map<K extends Comparable,V extends Comparable>
public boolean containsKey(Object key)
Comparable
.
containsKey
in interface Map<K extends Comparable,V extends Comparable>
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 nullpublic boolean containsValue(Object value)
Comparable
.
containsValue
in interface Map<K extends Comparable,V extends Comparable>
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 nullpublic V get(Object key)
Comparable
.
get
in interface Map<K extends Comparable,V extends Comparable>
key
- key whose associated value is to be returned
ClassCastException
- if the key is of an inappropriate type
NullPointerException
- if the key is nullpublic V put(K key, V value)
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 removedBoth key and value must implement
Comparable
.
put
in interface Map<K extends Comparable,V extends Comparable>
put
in interface BidiMap<K extends Comparable,V extends Comparable>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key
ClassCastException
- if the key is of an inappropriate type
NullPointerException
- if the key is nullpublic void putAll(Map<? extends K,? extends V> map)
Comparable
.
putAll
in interface Map<K extends Comparable,V extends Comparable>
map
- the map to copy frompublic V remove(Object key)
Comparable
.
remove
in interface Map<K extends Comparable,V extends Comparable>
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 nullpublic void clear()
clear
in interface Map<K extends Comparable,V extends Comparable>
public K getKey(V value)
Comparable
.
getKey
in interface BidiMap<K extends Comparable,V extends Comparable>
value
- value whose associated key is to be returned.
ClassCastException
- if the value is of an inappropriate type
NullPointerException
- if the value is nullpublic K removeValue(V value)
Comparable
.
removeValue
in interface BidiMap<K extends Comparable,V extends Comparable>
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 nullpublic K firstKey()
firstKey
in interface OrderedMap<K extends Comparable,V extends Comparable>
NoSuchElementException
- if this map is emptypublic K lastKey()
lastKey
in interface OrderedMap<K extends Comparable,V extends Comparable>
NoSuchElementException
- if this map is emptypublic K nextKey(K key)
Comparable
.
nextKey
in interface OrderedMap<K extends Comparable,V extends Comparable>
key
- the key to search for next from
public K previousKey(K key)
Comparable
.
previousKey
in interface OrderedMap<K extends Comparable,V extends Comparable>
key
- the key to search for previous from
public Set<K> keySet()
keySet
in interface Map<K extends Comparable,V extends Comparable>
public Collection<V> values()
values
in interface Map<K extends Comparable,V extends Comparable>
public Set<Map.Entry<K,V>> entrySet()
entrySet
in interface Map<K extends Comparable,V extends Comparable>
public MapIterator<K,V> mapIterator()
mapIterator
in interface BidiMap<K extends Comparable,V extends Comparable>
mapIterator
in interface IterableMap<K extends Comparable,V extends Comparable>
public OrderedMapIterator<K,V> orderedMapIterator()
orderedMapIterator
in interface OrderedMap<K extends Comparable,V extends Comparable>
public BidiMap<V,K> inverseBidiMap()
inverseBidiMap
in interface BidiMap<K extends Comparable,V extends Comparable>
inverseBidiMap
in interface OrderedBidiMap<K extends Comparable,V extends Comparable>
public OrderedBidiMap<V,K> inverseOrderedBidiMap()
inverseOrderedBidiMap
in interface OrderedBidiMap<K extends Comparable,V extends Comparable>
public boolean equals(Object obj)
equals
in interface Map<K extends Comparable,V extends Comparable>
equals
in class Object
obj
- the object to compare to
public int hashCode()
hashCode
in interface Map<K extends Comparable,V extends Comparable>
hashCode
in class Object
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |