Class TwoWayHashMap<K,​V>

  • Type Parameters:
    K - The key type.
    V - The value type.
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​V>
    Direct Known Subclasses:
    SequentialTwoWayHashMap

    public class TwoWayHashMap<K,​V>
    extends java.util.HashMap<K,​V>
    A hash map that has one-to-one relationship between keys and values. With the implementation of two hash maps instead of one, the lookup for keys with values is just as efficient as the lookup for values with keys.
    Since:
    Ptolemy II 6.1
    Version:
    $Id$
    Author:
    Thomas Huining Feng
    See Also:
    Serialized Form
    Pt.AcceptedRating:
    Red (tfeng)
    Pt.ProposedRating:
    Yellow (tfeng)
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      TwoWayHashMap()
      Construct an empty two-way hash map.
      TwoWayHashMap​(int initialCapacity)
      Construct an empty two-way hash map with an initial capacity.
      TwoWayHashMap​(int initialCapacity, float loadFactor)
      Construct an empty two-way hash map with an initial capacity and a load factor.
      TwoWayHashMap​(java.util.Map<? extends K,​? extends V> map)
      Construct a two-way hash map with the given hash map, which must have a one-to-one relationship between elements.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clear this two-way hash map.
      java.lang.Object clone()
      Clone this two-way hash map and return the clone.
      boolean containsValue​(java.lang.Object value)
      Test whether this two-way hash map contains the given value.
      K getKey​(java.lang.Object value)
      Given a value, return the corresponding key in this two-way hash map.
      V put​(K key, V value)
      Put a value into this two-way hash map and associate it with a key.
      V remove​(java.lang.Object key)
      Remove the value associated with the given key.
      java.util.Set<V> values()
      Return a set containing all the values in this two-way hash map.
      • Methods inherited from class java.util.HashMap

        compute, computeIfAbsent, computeIfPresent, containsKey, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll, size
      • Methods inherited from class java.util.AbstractMap

        equals, hashCode, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        equals, hashCode
    • Constructor Detail

      • TwoWayHashMap

        public TwoWayHashMap()
        Construct an empty two-way hash map.
      • TwoWayHashMap

        public TwoWayHashMap​(java.util.Map<? extends K,​? extends V> map)
        Construct a two-way hash map with the given hash map, which must have a one-to-one relationship between elements.
        Parameters:
        map - The hash map.
      • TwoWayHashMap

        public TwoWayHashMap​(int initialCapacity)
        Construct an empty two-way hash map with an initial capacity.
        Parameters:
        initialCapacity - The initial capacity.
      • TwoWayHashMap

        public TwoWayHashMap​(int initialCapacity,
                             float loadFactor)
        Construct an empty two-way hash map with an initial capacity and a load factor.
        Parameters:
        initialCapacity - The initial capacity.
        loadFactor - The load factor.
    • Method Detail

      • clear

        public void clear()
        Clear this two-way hash map.
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.HashMap<K,​V>
      • clone

        public java.lang.Object clone()
        Clone this two-way hash map and return the clone.
        Overrides:
        clone in class java.util.HashMap<K,​V>
        Returns:
        The clone.
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Test whether this two-way hash map contains the given value.
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Overrides:
        containsValue in class java.util.HashMap<K,​V>
        Parameters:
        value - The value.
        Returns:
        true if the value is contained.
      • getKey

        public K getKey​(java.lang.Object value)
        Given a value, return the corresponding key in this two-way hash map.
        Parameters:
        value - The value.
        Returns:
        the key.
      • put

        public V put​(K key,
                     V value)
        Put a value into this two-way hash map and associate it with a key.
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.HashMap<K,​V>
        Parameters:
        key - The key.
        value - The value.
        Returns:
        The value previously associated with the key in this two-way hash map.
      • remove

        public V remove​(java.lang.Object key)
        Remove the value associated with the given key.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.HashMap<K,​V>
        Parameters:
        key - The key.
        Returns:
        The removed value, or null if not found.
      • values

        public java.util.Set<V> values()
        Return a set containing all the values in this two-way hash map.
        Specified by:
        values in interface java.util.Map<K,​V>
        Overrides:
        values in class java.util.HashMap<K,​V>
        Returns:
        The set.