Class DecoratorReverseMap<K,V>

java.lang.Object
com.globalmentor.collections.MapDecorator<K,V>
com.globalmentor.collections.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>, 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 Details

    • DecoratorReverseMap

      public DecoratorReverseMap(Map<K,V> map, 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:
      NullPointerException - if the given map and/or reverse map is null.
  • Method Details

    • 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:
    • 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(Object value)
      Specified by:
      containsValue in interface 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 Map<K,V>
      Overrides:
      put in class MapDecorator<K,V>
    • remove

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

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