Class BiUniqueMap<K,V>

java.lang.Object
com.dianxin.tori.base.collections.BiUniqueMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
UniqueMap<K,V>

public class BiUniqueMap<K,V> extends Object implements UniqueMap<K,V>
A bidirectional implementation of the UniqueMap interface, backed by two HashMaps. This class ensures that both keys and values remain unique, allowing $O(1)$ time complexity for lookups in both directions (key-to-value and value-to-key). *

Note: While the putUnique(Object, Object) method is synchronized, other read and write operations are not. If multiple threads access this map concurrently, external synchronization may be required.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface com.dianxin.tori.base.collections.UniqueMap

    UniqueMap.Entry<K,V>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty BiUniqueMap with default initial capacity.
    BiUniqueMap(UniqueMap.Entry<K,V> simpleKeyValue)
    Constructs a BiUniqueMap initialized with a single key-value pair provided by the specified UniqueMap.Entry.
    Constructs a BiUniqueMap containing the same mappings as the specified map.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if this map contains a mapping for the specified key.
    boolean
    Returns true if this map maps one or more keys to the specified value.
    get(K key)
    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
    @NonNull Map<K,V>
    Returns an unmodifiable Map view of the mappings contained in this map.
    Returns the key associated with the specified value, or null if this map contains no mapping for the value.
    void
    putUnique(K key, V value)
    Associates the specified value with the specified key in this map.
    void
    Removes the mapping for a key from this map if it is present.
    void
    Removes the mapping for a value from this map if it is present.
    int
    Returns the number of key-value mappings in this map.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BiUniqueMap

      public BiUniqueMap()
      Constructs an empty BiUniqueMap with default initial capacity.
    • BiUniqueMap

      public BiUniqueMap(Map<K,V> map)
      Constructs a BiUniqueMap containing the same mappings as the specified map. If the input map contains duplicate keys or duplicate values, an IllegalArgumentException will be thrown. If the input map is null, this constructor returns an empty map.
      Parameters:
      map - the map whose mappings are to be placed in this unique map, may be null
      Throws:
      IllegalArgumentException - if the specified map contains duplicate values or keys
    • BiUniqueMap

      public BiUniqueMap(UniqueMap.Entry<K,V> simpleKeyValue)
      Constructs a BiUniqueMap initialized with a single key-value pair provided by the specified UniqueMap.Entry. If the input parameter is null, this constructor returns an empty map.
      Parameters:
      simpleKeyValue - the single key-value container used to populate this map, may be null
  • Method Details

    • putUnique

      public void putUnique(K key, V value)
      Description copied from interface: UniqueMap
      Associates the specified value with the specified key in this map. Both the key and the value must be unique within the map.
      Specified by:
      putUnique in interface UniqueMap<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
    • get

      public V get(K key)
      Description copied from interface: UniqueMap
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      Specified by:
      get in interface UniqueMap<K,V>
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the value to which the specified key is mapped, or null
    • getKeyByValue

      public K getKeyByValue(V value)
      Description copied from interface: UniqueMap
      Returns the key associated with the specified value, or null if this map contains no mapping for the value.
      Specified by:
      getKeyByValue in interface UniqueMap<K,V>
      Parameters:
      value - the value whose associated key is to be returned
      Returns:
      the key associated with the specified value, or null
    • containsKey

      public boolean containsKey(K key)
      Description copied from interface: UniqueMap
      Returns true if this map contains a mapping for the specified key.
      Specified by:
      containsKey in interface UniqueMap<K,V>
      Parameters:
      key - the key whose presence in this map is to be tested
      Returns:
      true if this map contains a mapping for the specified key
    • containsValue

      public boolean containsValue(V value)
      Description copied from interface: UniqueMap
      Returns true if this map maps one or more keys to the specified value.
      Specified by:
      containsValue in interface UniqueMap<K,V>
      Parameters:
      value - the value whose presence in this map is to be tested
      Returns:
      true if this map maps one or more keys to the specified value
    • removeByKey

      public void removeByKey(K key)
      Description copied from interface: UniqueMap
      Removes the mapping for a key from this map if it is present. Also removes the corresponding value from the bidirectional mapping.
      Specified by:
      removeByKey in interface UniqueMap<K,V>
      Parameters:
      key - the key whose mapping is to be removed from the map
    • removeByValue

      public void removeByValue(V value)
      Description copied from interface: UniqueMap
      Removes the mapping for a value from this map if it is present. Also removes the corresponding key from the bidirectional mapping.
      Specified by:
      removeByValue in interface UniqueMap<K,V>
      Parameters:
      value - the value whose mapping is to be removed from the map
    • size

      public int size()
      Description copied from interface: UniqueMap
      Returns the number of key-value mappings in this map.
      Specified by:
      size in interface UniqueMap<K,V>
      Returns:
      the number of key-value mappings in this map
    • getAll

      public @NonNull Map<K,V> getAll()
      Description copied from interface: UniqueMap
      Returns an unmodifiable Map view of the mappings contained in this map.
      Specified by:
      getAll in interface UniqueMap<K,V>
      Returns:
      an unmodifiable map containing all key-value pairs