Class DecoratorReverseMap<K,​V>

  • Type Parameters:
    K - The type of map key.
    V - The type of map value.

    The keys and values in this map have a one-to-one relationship. Associating multiple values with a key will likely result in errant functionality.

    All Implemented Interfaces:
    ReverseMap<K,​V>, java.util.Map<K,​V>

    public class DecoratorReverseMap<K,​V>
    extends MapDecorator<K,​V>
    implements ReverseMap<K,​V>
    A map that also allows lookup of the map keys keyed to the values by decorating two maps.
    Author:
    Garret Wilson
    • Constructor Detail

      • DecoratorReverseMap

        public DecoratorReverseMap​(java.util.Map<K,​V> map,
                                   java.util.Map<V,​K> reverseMap)
        Constructs a reverse map by decorating two other maps.
        Parameters:
        map - The main map to be decorated.
        reverseMap - The map to contain reverse lookup values.
        Throws:
        java.lang.NullPointerException - if the given map and/or reverse map is null.
    • Method Detail

      • getKey

        public K getKey​(V value)
        Returns the key that represents the given value.
        Specified by:
        getKey in interface ReverseMap<K,​V>
        Parameters:
        value - The value whose associated key is to be returned.
        Returns:
        The key to which this map reverse maps the specified value, or null if the map contains no reverse mapping for this value.
        See Also:
        containsValue(Object)
      • removeValue

        public K removeValue​(V value)
        Description copied from interface: ReverseMap
        Removes the mapping for a value from this map if it is present.
        Specified by:
        removeValue in interface ReverseMap<K,​V>
        Parameters:
        value - The value whose mapping is to be removed from the map.
        Returns:
        The previous key associated with the value, or null if there was no mapping for the value.
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Overrides:
        containsValue in class MapDecorator<K,​V>
        Implementation Specification:
        This version uses an internal reverse map to provide faster lookups than the default linear-time lookup.
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class MapDecorator<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class MapDecorator<K,​V>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class MapDecorator<K,​V>