org.knowceans.map
Class BijectiveHashMap<X,Y>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<X,Y>
org.knowceans.map.BijectiveHashMap<X,Y>
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, java.util.Map<X,Y>, IBijectiveMap<X,Y>
public class BijectiveHashMap<X,Y>
- extends java.util.HashMap<X,Y>
- implements IBijectiveMap<X,Y>
BijectiveHashMap is a HashMap that bijectively assigns unique keys to unique
values and vice versa. The inverse mapping is done by a HashMap that maps
values to keys. With getInverse(), a key can be found from the value without
the search overhead of a value search. The bijective property constrains all
input to obey unique keys AND unique values, while permitting the
null element.
In relational terms, this class implements a 1:1 relation.
As the underlying HashMap has constant-time, O(1), get() and put()
complexity, it can be used to identify numerical dimensions, such as the rows
and columns of a matrix, much more conveniently than by array lookups (which
would require array search for the inverse), however with some additive
overhead over array index lookups.
- Author:
- heinrich
- See Also:
- Serialized Form
Nested classes/interfaces inherited from class java.util.AbstractMap |
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V> |
Nested classes/interfaces inherited from interface java.util.Map |
java.util.Map.Entry<K,V> |
Method Summary |
void |
clear()
|
java.util.HashMap<Y,X> |
getInverse()
|
X |
getInverse(Y val)
gets key for a value. |
java.util.Set<Y> |
getValues()
returns the keys of the inverse map. |
static void |
main(java.lang.String[] args)
|
Y |
put(X key,
Y val)
put key-value pair into the map. |
Y |
remove(java.lang.Object key)
removes the key and its value from the map. |
java.util.Collection<Y> |
values()
|
Methods inherited from class java.util.HashMap |
clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, putAll, size |
Methods inherited from class java.util.AbstractMap |
equals, hashCode, toString |
Methods inherited from class java.lang.Object |
getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Map |
containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, putAll, size |
BijectiveHashMap
public BijectiveHashMap()
BijectiveHashMap
public BijectiveHashMap(java.util.Map<? extends X,? extends Y> t)
BijectiveHashMap
public BijectiveHashMap(int initialCapacity)
main
public static void main(java.lang.String[] args)
clear
public void clear()
- Specified by:
clear
in interface java.util.Map<X,Y>
- Overrides:
clear
in class java.util.HashMap<X,Y>
put
public Y put(X key,
Y val)
- put key-value pair into the map. If the key exists, it is replaced, along
with the value it points to. If the value exists, it is replaced, along
with the key it pointed to. The method returns the old value that the key
pointed to. This is somewhat asymmetric because if a key gets
overwritten, there is no reaction. Thus, check before calling the method.
- Specified by:
put
in interface java.util.Map<X,Y>
- Overrides:
put
in class java.util.HashMap<X,Y>
remove
public Y remove(java.lang.Object key)
- removes the key and its value from the map. In the inverse map, the value
is removed. Returns the value
- Specified by:
remove
in interface java.util.Map<X,Y>
- Overrides:
remove
in class java.util.HashMap<X,Y>
getInverse
public X getInverse(Y val)
- gets key for a value.
- Specified by:
getInverse
in interface IBijectiveMap<X,Y>
- Parameters:
val
-
- Returns:
getInverse
public java.util.HashMap<Y,X> getInverse()
- Specified by:
getInverse
in interface IBijectiveMap<X,Y>
- Returns:
- the complete inverse HashMap
getValues
public java.util.Set<Y> getValues()
- returns the keys of the inverse map. Use this preferably over values().
- Returns:
values
public java.util.Collection<Y> values()
- Specified by:
values
in interface java.util.Map<X,Y>
- Overrides:
values
in class java.util.HashMap<X,Y>