Class HashBiMap<K,V>

All Implemented Interfaces:
BiMap<K,V>, Serializable, Map<K,V>

public final class HashBiMap<K,V> extends Maps.IteratorBasedAbstractMap<K,V> implements BiMap<K,V>, Serializable
A BiMap backed by two hash tables. This implementation allows null keys and values. A HashBiMap and its inverse are both serializable.

This implementation guarantees insertion-based iteration order of its keys.

See the Guava User Guide article on BiMap .

Since:
2.0
See Also:
  • Field Details

    • LOAD_FACTOR

      private static final double LOAD_FACTOR
      See Also:
    • hashTableKToV

      private transient HashBiMap.BiEntry<K,V>[] hashTableKToV
    • hashTableVToK

      private transient HashBiMap.BiEntry<K,V>[] hashTableVToK
    • firstInKeyInsertionOrder

      @CheckForNull private transient HashBiMap.BiEntry<K,V> firstInKeyInsertionOrder
    • lastInKeyInsertionOrder

      @CheckForNull private transient HashBiMap.BiEntry<K,V> lastInKeyInsertionOrder
    • size

      private transient int size
    • mask

      private transient int mask
    • modCount

      private transient int modCount
    • inverse

      @CheckForNull private transient BiMap<V,K> inverse
    • serialVersionUID

      private static final long serialVersionUID
      See Also:
  • Constructor Details

    • HashBiMap

      private HashBiMap(int expectedSize)
  • Method Details

    • create

      public static <K, V> HashBiMap<K,V> create()
      Returns a new, empty HashBiMap with the default initial capacity (16).
    • create

      public static <K, V> HashBiMap<K,V> create(int expectedSize)
      Constructs a new, empty bimap with the specified expected size.
      Parameters:
      expectedSize - the expected number of entries
      Throws:
      IllegalArgumentException - if the specified expected size is negative
    • create

      public static <K, V> HashBiMap<K,V> create(Map<? extends K,? extends V> map)
      Constructs a new bimap containing initial values from map. The bimap is created with an initial capacity sufficient to hold the mappings in the specified map.
    • init

      private void init(int expectedSize)
    • delete

      private void delete(HashBiMap.BiEntry<K,V> entry)
      Finds and removes entry from the bucket linked lists in both the key-to-value direction and the value-to-key direction.
    • insert

      private void insert(HashBiMap.BiEntry<K,V> entry, @CheckForNull HashBiMap.BiEntry<K,V> oldEntryForKey)
    • seekByKey

      @CheckForNull private HashBiMap.BiEntry<K,V> seekByKey(@CheckForNull Object key, int keyHash)
    • seekByValue

      @CheckForNull private HashBiMap.BiEntry<K,V> seekByValue(@CheckForNull Object value, int valueHash)
    • containsKey

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

      public boolean containsValue(@CheckForNull Object value)
      Returns true if this BiMap contains an entry whose value is equal to value (or, equivalently, if this inverse view contains a key that is equal to value).

      Due to the property that values in a BiMap are unique, this will tend to execute in faster-than-linear time.

      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
      Parameters:
      value - the object to search for in the values of this BiMap
      Returns:
      true if a mapping exists from a key to the specified value
    • get

      @CheckForNull public V get(@CheckForNull Object key)
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
    • put

      @CheckForNull public V put(K key, V value)
      Description copied from interface: BiMap
      Specified by:
      put in interface BiMap<K,V>
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
    • put

      @CheckForNull private V put(K key, V value, boolean force)
    • forcePut

      @CheckForNull public V forcePut(K key, V value)
      Description copied from interface: BiMap
      An alternate form of put that silently removes any existing entry with the value value before proceeding with the BiMap.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.

      Specified by:
      forcePut in interface BiMap<K,V>
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Returns:
      the value that was previously associated with the key, or null if there was no previous entry. (If the bimap contains null values, then forcePut, like put, returns null both if the key is absent and if it is present with a null value.)
    • putInverse

      @CheckForNull private K putInverse(V value, K key, boolean force)
    • rehashIfNecessary

      private void rehashIfNecessary()
    • createTable

      private HashBiMap.BiEntry<K,V>[] createTable(int length)
    • remove

      @CheckForNull public V remove(@CheckForNull Object key)
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class Maps.IteratorBasedAbstractMap<K,V>
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
      Specified by:
      size in class Maps.IteratorBasedAbstractMap<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
      Overrides:
      keySet in class AbstractMap<K,V>
    • values

      public Set<V> values()
      Description copied from interface: BiMap

      Because a bimap has unique values, this method returns a Set, instead of the Collection specified in the Map interface.

      Specified by:
      values in interface BiMap<K,V>
      Specified by:
      values in interface Map<K,V>
      Overrides:
      values in class AbstractMap<K,V>
    • entryIterator

      Iterator<Map.Entry<K,V>> entryIterator()
      Specified by:
      entryIterator in class Maps.IteratorBasedAbstractMap<K,V>
    • forEach

      public void forEach(BiConsumer<? super K,? super V> action)
      Specified by:
      forEach in interface Map<K,V>
    • replaceAll

      public void replaceAll(BiFunction<? super K,? super V,? extends V> function)
      Specified by:
      replaceAll in interface Map<K,V>
    • inverse

      public BiMap<V,K> inverse()
      Description copied from interface: BiMap
      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.

      Note:There is no guaranteed correspondence between the iteration order of a bimap and that of its inverse.

      Specified by:
      inverse in interface BiMap<K,V>
      Returns:
      the inverse view of this bimap
    • writeObject

      private void writeObject(ObjectOutputStream stream) throws IOException
      Throws:
      IOException
    • readObject

      private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
      Throws:
      IOException
      ClassNotFoundException