Class BiMap<K,V>

java.lang.Object
com.amalgamasimulation.utils.container.BiMap<K,V>
Type Parameters:
K - the type of keys maintained by this BiMap
V - the type of mapped values
All Implemented Interfaces:
Map<K,V>

public class BiMap<K,V> extends Object implements 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
  • Constructor Details

    • BiMap

      public BiMap()
  • Method Details

    • value

      public V value(K key)
      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 null if this map contains no mapping for the key
    • key

      public K key(V value)
      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 null if this map contains no mapping for the value
    • keyOrDefault

      public K keyOrDefault(V value, K def)
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
    • removeKey

      public V removeKey(K key)
      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, or null if there was no mapping for key
    • removeValue

      public K removeValue(V value)
      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, or null if there was no mapping for value
    • keys

      public List<K> 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

      public List<V> values()
      Returns a read-only list of all the values in this map, in the order they were inserted.
      Specified by:
      values in interface Map<K,V>
      Returns:
      a list of all the values in this map
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>