K
- the type of the keys in the mapV
- the type of the values in the mappublic abstract class AbstractDualBidiMap<K,V> extends Object implements BidiMap<K,V>
BidiMap
implemented using two maps.
An implementation can be written simply by implementing the
createBidiMap(Map, Map, BidiMap)
method.
DualHashBidiMap
,
DualTreeBidiMap
Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractDualBidiMap.BidiMapIterator<K,V>
Inner class MapIterator.
|
protected static class |
AbstractDualBidiMap.EntrySet<K,V>
Inner class EntrySet.
|
protected static class |
AbstractDualBidiMap.EntrySetIterator<K,V>
Inner class EntrySetIterator.
|
protected static class |
AbstractDualBidiMap.KeySet<K>
Inner class KeySet.
|
protected static class |
AbstractDualBidiMap.KeySetIterator<K>
Inner class KeySetIterator.
|
protected static class |
AbstractDualBidiMap.MapEntry<K,V>
Inner class MapEntry.
|
protected static class |
AbstractDualBidiMap.Values<V>
Inner class Values.
|
protected static class |
AbstractDualBidiMap.ValuesIterator<V>
Inner class ValuesIterator.
|
protected static class |
AbstractDualBidiMap.View<K,V,E>
Inner class View.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractDualBidiMap()
Creates an empty map, initialised by
createMap . |
protected |
AbstractDualBidiMap(Map<K,V> normalMap,
Map<V,K> reverseMap)
Creates an empty map using the two maps specified as storage.
|
protected |
AbstractDualBidiMap(Map<K,V> normalMap,
Map<V,K> reverseMap,
BidiMap<V,K> inverseBidiMap)
Constructs a map that decorates the specified maps,
used by the subclass
createBidiMap implementation. |
Modifier and Type | Method and Description |
---|---|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
protected abstract BidiMap<V,K> |
createBidiMap(Map<V,K> normalMap,
Map<K,V> reverseMap,
BidiMap<K,V> inverseMap)
Creates a new instance of the subclass.
|
protected Iterator<Map.Entry<K,V>> |
createEntrySetIterator(Iterator<Map.Entry<K,V>> iterator)
Creates an entry set iterator.
|
protected Iterator<K> |
createKeySetIterator(Iterator<K> iterator)
Creates a key set iterator.
|
protected Iterator<V> |
createValuesIterator(Iterator<V> iterator)
Creates a values iterator.
|
Set<Map.Entry<K,V>> |
entrySet()
Gets an entrySet view of the map.
|
boolean |
equals(Object obj) |
V |
get(Object key) |
K |
getKey(Object value)
Gets the key that is currently mapped to the specified value.
|
int |
hashCode() |
BidiMap<V,K> |
inverseBidiMap()
Gets a view of this map where the keys and values are reversed.
|
boolean |
isEmpty() |
Set<K> |
keySet()
Gets a keySet view of the map.
|
MapIterator<K,V> |
mapIterator()
Obtains a
MapIterator over the map. |
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) |
V |
remove(Object key) |
K |
removeValue(Object value)
Removes the key-value pair that is currently mapped to the specified
value (optional operation).
|
int |
size() |
String |
toString() |
Set<V> |
values()
Gets a values view of the map.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
protected AbstractDualBidiMap()
createMap
.
This constructor remains in place for deserialization.
All other usage is deprecated in favour of
AbstractDualBidiMap(Map, Map)
.
protected AbstractDualBidiMap(Map<K,V> normalMap, Map<V,K> reverseMap)
The two maps must be a matching pair, normal and reverse. They will typically both be empty.
Neither map is validated, so nulls may be passed in.
If you choose to do this then the subclass constructor must populate
the maps[]
instance variable itself.
normalMap
- the normal direction mapreverseMap
- the reverse direction mapprotected AbstractDualBidiMap(Map<K,V> normalMap, Map<V,K> reverseMap, BidiMap<V,K> inverseBidiMap)
createBidiMap
implementation.normalMap
- the normal direction mapreverseMap
- the reverse direction mapinverseBidiMap
- the inverse BidiMapprotected abstract BidiMap<V,K> createBidiMap(Map<V,K> normalMap, Map<K,V> reverseMap, BidiMap<K,V> inverseMap)
normalMap
- the normal direction mapreverseMap
- the reverse direction mapinverseMap
- this map, which is the inverse in the new mappublic int size()
public boolean isEmpty()
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in interface Get<K,V>
key
- key whose presence in this map is to be testedtrue
if this map contains a mapping for the specified
keyMap.containsKey(Object)
public int hashCode()
public V put(K key, V value)
BidiMap
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 DualHashBidiMap(); 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 DualHashBidiMap(); 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
public void clear()
public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
containsValue
in interface Get<K,V>
value
- value whose presence in this map is to be testedtrue
if this map maps one or more keys to the
specified valueMap.containsValue(Object)
public MapIterator<K,V> mapIterator()
MapIterator
over the map.
The iterator implements ResetableMapIterator
.
This implementation relies on the entrySet iterator.
The setValue() methods only allow a new value to be set. If the value being set is already in the map, an IllegalArgumentException is thrown (as setValue cannot change the size of the map).
mapIterator
in interface IterableGet<K,V>
public K getKey(Object value)
BidiMap
If the value is not contained in the map, null
is returned.
Implementations should seek to make this method perform equally as well
as get(Object)
.
public K removeValue(Object value)
BidiMap
If the value is not contained in the map, null
is returned.
Implementations should seek to make this method perform equally as well
as remove(Object)
.
removeValue
in interface BidiMap<K,V>
value
- the value to find the key-value pair fornull
if nothing removedpublic BidiMap<V,K> inverseBidiMap()
BidiMap
Changes to one map will be visible in the other and vice versa.
This enables both directions of the map to be accessed as a Map
.
Implementations should seek to avoid creating a new object every time this
method is called. See AbstractMap.values()
etc. Calling this
method on the inverse map should return the original.
inverseBidiMap
in interface BidiMap<K,V>
public Set<K> keySet()
protected Iterator<K> createKeySetIterator(Iterator<K> iterator)
iterator
- the iterator to decoratepublic Set<V> values()
protected Iterator<V> createValuesIterator(Iterator<V> iterator)
iterator
- the iterator to decoratepublic Set<Map.Entry<K,V>> entrySet()
The Map Entry setValue() method only allow a new value to be set. If the value being set is already in the map, an IllegalArgumentException is thrown (as setValue cannot change the size of the map).
protected Iterator<Map.Entry<K,V>> createEntrySetIterator(Iterator<Map.Entry<K,V>> iterator)
iterator
- the iterator to decorateCopyright © 2001–2019 The Apache Software Foundation. All rights reserved.