Class BiMap<K,V>

java.lang.Object
com.landawn.abacus.util.BiMap<K,V>
Type Parameters:
K - the key type
V - the value type
All Implemented Interfaces:
Map<K,V>

public final class BiMap<K,V> extends Object implements Map<K,V>
A BiMap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables BiMaps to support an "inverse view", which is another BiMap containing the same entries as this BiMap but with reversed keys and values.
Since:
0.8
Author:
Haiyang Li
  • Constructor Details

    • BiMap

      public BiMap()
    • BiMap

      public BiMap(int initialCapacity)
    • BiMap

      public BiMap(int initialCapacity, float loadFactor)
    • BiMap

      public BiMap(Class<? extends Map> keyMapType, Class<? extends Map> valueMapType)
    • BiMap

      public BiMap(Supplier<? extends Map<K,V>> keyMapSupplier, Supplier<? extends Map<V,K>> valueMapSupplier)
  • Method Details

    • of

      public static <K, V> BiMap<K,V> of(K k1, V v1)
      Type Parameters:
      K - the key type
      V - the value type
      k -
      v -
      Parameters:
      k1 -
      v1 -
      Returns:
    • of

      public static <K, V> BiMap<K,V> of(K k1, V v1, K k2, V v2)
      Type Parameters:
      K - the key type
      V - the value type
      k -
      v -
      Parameters:
      k1 -
      v1 -
      k2 -
      v2 -
      Returns:
    • of

      public static <K, V> BiMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
      Type Parameters:
      K - the key type
      V - the value type
      k -
      v -
      Parameters:
      k1 -
      v1 -
      k2 -
      v2 -
      k3 -
      v3 -
      Returns:
    • of

      public static <K, V> BiMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
      Type Parameters:
      K - the key type
      V - the value type
      k -
      v -
      Parameters:
      k1 -
      v1 -
      k2 -
      v2 -
      k3 -
      v3 -
      k4 -
      v4 -
      Returns:
    • of

      public static <K, V> BiMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
      Type Parameters:
      K - the key type
      V - the value type
      k -
      v -
      Parameters:
      k1 -
      v1 -
      k2 -
      v2 -
      k3 -
      v3 -
      k4 -
      v4 -
      k5 -
      v5 -
      Returns:
    • of

      public static <K, V> BiMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
      Type Parameters:
      K - the key type
      V - the value type
      k -
      v -
      Parameters:
      k1 -
      v1 -
      k2 -
      v2 -
      k3 -
      v3 -
      k4 -
      v4 -
      k5 -
      v5 -
      k6 -
      v6 -
      Returns:
    • of

      public static <K, V> BiMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
      Type Parameters:
      K - the key type
      V - the value type
      k -
      v -
      Parameters:
      k1 -
      v1 -
      k2 -
      v2 -
      k3 -
      v3 -
      k4 -
      v4 -
      k5 -
      v5 -
      k6 -
      v6 -
      k7 -
      v7 -
      Returns:
    • copyOf

      public static <K, V> BiMap<K,V> copyOf(Map<? extends K,? extends V> map)
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      map -
      Returns:
    • from

      @Deprecated public static <K, V> BiMap<K,V> from(Map<? extends K,? extends V> map)
      Deprecated.
      replaced by copyOf
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      map -
      Returns:
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Parameters:
      key -
      Returns:
    • getByValue

      public K getByValue(Object value)
      Gets the by value.
      Parameters:
      value -
      Returns:
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
      Parameters:
      key -
      value -
      Returns:
      Throws:
      IllegalArgumentException - if the given value is already bound to a different key in this bimap. The bimap will remain unmodified in this event. To avoid this exception, call forcePut(K, V) instead.
    • forcePut

      public V forcePut(K key, V value)
      An alternate form of put that silently removes any existing entry with the value value before proceeding with the put(K, V) operation. If the bimap previously contained the provided key-value mapping, this method has no effect.

      Note that a successful call to this method could cause the size of the bimap to increase by one, stay the same, or even decrease by one.

      Warning: If an existing entry with this value is removed, the key for that entry is discarded and not returned.

      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Returns:
      be null, or null if there was no previous entry
    • putAll

      public void putAll(Map<? extends K,? extends V> m)

      Warning: the results of calling this method may vary depending on the iteration order of map.

      Specified by:
      putAll in interface Map<K,V>
      Parameters:
      m -
      Throws:
      IllegalArgumentException - if an attempt to put any entry fails. Note that some map entries may have been added to the bimap before the exception was thrown.
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
      Parameters:
      key -
      Returns:
    • removeByValue

      public K removeByValue(Object value)
      Removes the by value.
      Parameters:
      value -
      Returns:
    • containsKey

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

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
      Parameters:
      value -
      Returns:
    • keySet

      public ImmutableSet<K> keySet()
      Returns an immutable key set.
      Specified by:
      keySet in interface Map<K,V>
      Returns:
    • values

      public ImmutableSet<V> values()
      Returns an immutable value set.
      Specified by:
      values in interface Map<K,V>
      Returns:
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Returns an immutable Set of Immutable entry.
      Specified by:
      entrySet in interface Map<K,V>
      Returns:
    • inversed

      public BiMap<V,K> inversed()
      Returns the inverse view of this BiMap, which maps each of this bimap's values to its associated key. The two BiMaps are backed by the same data; any changes to one will appear in the other.
      Returns:
    • copy

      public BiMap<K,V> copy()
    • clear

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

      public boolean isEmpty()
      Checks if is empty.
      Specified by:
      isEmpty in interface Map<K,V>
      Returns:
      true, if is empty
    • size

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

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
      Parameters:
      obj -
      Returns:
    • toString

      public String toString()
      Overrides:
      toString in class Object