Class BiUniqueMap<K,V>
java.lang.Object
com.dianxin.tori.base.collections.BiUniqueMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
UniqueMap<K,V>
A bidirectional implementation of the
UniqueMap interface, backed by two HashMaps.
This class ensures that both keys and values remain unique, allowing $O(1)$ time complexity
for lookups in both directions (key-to-value and value-to-key).
* Note: While the putUnique(Object, Object) method is synchronized,
other read and write operations are not. If multiple threads access this map concurrently,
external synchronization may be required.
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.dianxin.tori.base.collections.UniqueMap
UniqueMap.Entry<K,V> -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an emptyBiUniqueMapwith default initial capacity.BiUniqueMap(UniqueMap.Entry<K, V> simpleKeyValue) Constructs aBiUniqueMapinitialized with a single key-value pair provided by the specifiedUniqueMap.Entry.BiUniqueMap(Map<K, V> map) Constructs aBiUniqueMapcontaining the same mappings as the specified map. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(K key) Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(V value) Returnstrueif this map maps one or more keys to the specified value.Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.getAll()Returns an unmodifiableMapview of the mappings contained in this map.getKeyByValue(V value) Returns the key associated with the specified value, ornullif this map contains no mapping for the value.voidAssociates the specified value with the specified key in this map.voidremoveByKey(K key) Removes the mapping for a key from this map if it is present.voidremoveByValue(V value) Removes the mapping for a value from this map if it is present.intsize()Returns the number of key-value mappings in this map.
-
Constructor Details
-
BiUniqueMap
public BiUniqueMap()Constructs an emptyBiUniqueMapwith default initial capacity. -
BiUniqueMap
Constructs aBiUniqueMapcontaining the same mappings as the specified map. If the input map contains duplicate keys or duplicate values, anIllegalArgumentExceptionwill be thrown. If the input map isnull, this constructor returns an empty map.- Parameters:
map- the map whose mappings are to be placed in this unique map, may benull- Throws:
IllegalArgumentException- if the specified map contains duplicate values or keys
-
BiUniqueMap
Constructs aBiUniqueMapinitialized with a single key-value pair provided by the specifiedUniqueMap.Entry. If the input parameter isnull, this constructor returns an empty map.- Parameters:
simpleKeyValue- the single key-value container used to populate this map, may benull
-
-
Method Details
-
putUnique
Description copied from interface:UniqueMapAssociates the specified value with the specified key in this map. Both the key and the value must be unique within the map. -
get
Description copied from interface:UniqueMapReturns the value to which the specified key is mapped, ornullif this map contains no mapping for the key. -
getKeyByValue
Description copied from interface:UniqueMapReturns the key associated with the specified value, ornullif this map contains no mapping for the value.- Specified by:
getKeyByValuein interfaceUniqueMap<K,V> - Parameters:
value- the value whose associated key is to be returned- Returns:
- the key associated with the specified value, or
null
-
containsKey
Description copied from interface:UniqueMapReturnstrueif this map contains a mapping for the specified key.- Specified by:
containsKeyin interfaceUniqueMap<K,V> - Parameters:
key- the key whose presence in this map is to be tested- Returns:
trueif this map contains a mapping for the specified key
-
containsValue
Description copied from interface:UniqueMapReturnstrueif this map maps one or more keys to the specified value.- Specified by:
containsValuein interfaceUniqueMap<K,V> - Parameters:
value- the value whose presence in this map is to be tested- Returns:
trueif this map maps one or more keys to the specified value
-
removeByKey
Description copied from interface:UniqueMapRemoves the mapping for a key from this map if it is present. Also removes the corresponding value from the bidirectional mapping.- Specified by:
removeByKeyin interfaceUniqueMap<K,V> - Parameters:
key- the key whose mapping is to be removed from the map
-
removeByValue
Description copied from interface:UniqueMapRemoves the mapping for a value from this map if it is present. Also removes the corresponding key from the bidirectional mapping.- Specified by:
removeByValuein interfaceUniqueMap<K,V> - Parameters:
value- the value whose mapping is to be removed from the map
-
size
public int size()Description copied from interface:UniqueMapReturns the number of key-value mappings in this map. -
getAll
Description copied from interface:UniqueMapReturns an unmodifiableMapview of the mappings contained in this map.
-