Class BiMap<K,V>
java.lang.Object
com.amalgamasimulation.utils.container.BiMap<K,V>
- Type Parameters:
K- the type of keys maintained by this BiMapV- the type of mapped values
- All Implemented Interfaces:
Map<K,V>
A bidirectional map (BiMap) implementation that allows lookup by key to get a
value and lookup by value to get a key. This class implements the
Map
interface, enabling all the functionalities of a standard map, with the added
capability of reverse lookup, i.e., finding keys by their associated values.
Underneath, it uses two HashMap instances: one for the key-to-value
mapping and another for the value-to-key mapping, ensuring efficient lookups
in both directions.
- Author:
- Andrey Malykhanov
-
Nested Class Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleanisEmpty()Retrieves the key to which the specified value is mapped.keyOrDefault(V value, K def) keys()Returns a read-only list of all the keys in this map, in the order they were inserted.keySet()voidRemoves the mapping for a key from this map if it is present.removeValue(V value) Removes the mapping for a value from this map if it is present.intsize()Retrieves the value to which the specified key is mapped.values()Returns a read-only list of all the values in this map, in the order they were inserted.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
BiMap
public BiMap()
-
-
Method Details
-
value
Retrieves the value to which the specified key is mapped.- Parameters:
key- the key whose associated value is to be returned- Returns:
- the value to which the specified key is mapped, or
nullif this map contains no mapping for the key
-
key
Retrieves the key to which the specified value is mapped.- Parameters:
value- the value whose associated key is to be returned- Returns:
- the key to which the specified value is mapped, or
nullif this map contains no mapping for the value
-
keyOrDefault
-
put
-
removeKey
Removes the mapping for a key from this map if it is present. The method also removes the corresponding reverse mapping from the value to the key.- Parameters:
key- key whose mapping is to be removed from the map- Returns:
- the previous value associated with
key, ornullif there was no mapping forkey
-
removeValue
Removes the mapping for a value from this map if it is present. The method also removes the corresponding reverse mapping from the key to the value.- Parameters:
value- value whose mapping is to be removed from the map- Returns:
- the key that was associated with
value, ornullif there was no mapping forvalue
-
keys
Returns a read-only list of all the keys in this map, in the order they were inserted.- Returns:
- a list of all the keys in this map
-
values
Returns a read-only list of all the values in this map, in the order they were inserted. -
clear
public void clear() -
containsKey
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V>
-
entrySet
-
get
-
isEmpty
public boolean isEmpty() -
keySet
-
putAll
-
size
public int size() -
remove
-