Class BiMap<K,V>

java.lang.Object
com.pnfsoftware.jeb.util.collect.BiMap<K,V>
Type Parameters:
K - key type; must have correct equals/hashCode
V - value type; must have correct equals/hashCode

@Ser public class BiMap<K,V> extends Object
A bidirectional map of key-values. This class is serializable. The null key and null value are forbidden. The key-value mapping is one-to-one: a key can only map to one value, and one value can only map to one key. (One consequence is that putting a key-value pair may either increment, keep the same, or even decrement the map size.

Thread-safety: none.

Note: this class does not implement the Map interface, but attempts to follow its guidelines and contracts the best it can. One notable exception is that this class does not override hashCode/equals.

  • Constructor Details

    • BiMap

      public BiMap()
      Create a new bi-map.
    • BiMap

      public BiMap(BiMap<K,V> o)
      Create a copy of an existing bi-map.
      Parameters:
      o - source bi-map
    • BiMap

      public BiMap(CollectionOrder keyOrder)
      Create a new bi-map.
      Parameters:
      keyOrder - key ordering
  • Method Details

    • clear

      public void clear()
      Remove all mappings.
    • isEmpty

      public boolean isEmpty()
      Determine whether this map is empty.
      Returns:
      true if no mapping exists
    • size

      public int size()
      Get the number of mappings.
      Returns:
      number of mappings
    • put

      public V put(K key, V value)
      Add a one-to-one key-value mapping.
      Parameters:
      key - non-null key
      value - non-null value
      Returns:
      value previously associated with the key, or null
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Add all mappings from a map.
      Parameters:
      m - source map
    • containsKey

      public boolean containsKey(K key)
      Determine whether a key is present.
      Parameters:
      key - key
      Returns:
      true if the key is present
    • containsValue

      public boolean containsValue(V value)
      Determine whether a value is present.
      Parameters:
      value - value
      Returns:
      true if the value is present
    • get

      public V get(K key)
      Retrieve a value by key.
      Parameters:
      key - key
      Returns:
      value associated with the key, or null
    • getKeyForValue

      public K getKeyForValue(V value)
      Retrieve a key by value.
      Parameters:
      value - value
      Returns:
      key associated with the value, or null
    • remove

      public V remove(K key)
      Remove a mapping by key.
      Parameters:
      key - key
      Returns:
      removed value, or null
    • removeValue

      public K removeValue(V value)
      Remove a mapping by value.
      Parameters:
      value - value
      Returns:
      removed key, or null
    • keySet

      public Set<K> keySet()
      Get a view of the keys.
      Returns:
      key set view
    • values

      public Set<V> values()
      Get a view of the values.
      Returns:
      value set view
    • asMap

      public Map<K,V> asMap()
      Get a read-only view of the forward map.
      Returns:
      read-only forward map
    • asReverseMap

      public Map<V,K> asReverseMap()
      Get a read-only view of the reverse map.
      Returns:
      read-only reverse map
    • toString

      public String toString()
      Overrides:
      toString in class Object