Interface UniqueMap<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Known Implementing Classes:
BiUniqueMap, ConcurrentUniqueMap

@NullMarked public interface UniqueMap<K,V>
A map that enforces a one-to-one (bijective) mapping between keys and values. Unlike a standard Map, this structure guarantees that both keys and values are unique. It allows bidirectional lookups: finding a value by its key, or finding a key by its value.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final record 
    A simple immutable record representing a key-value pair.
  • 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.
    @Unmodifiable 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.
    static <R1, R2> UniqueMap<R1,R2>
    of(UniqueMap.Entry<R1,R2>... entries)
    Returns an unmodifiable UniqueMap containing the given key-value mappings.
    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.
  • Method Details

    • putUnique

      void putUnique(K key, V value)
      Associates the specified value with the specified key in this map. Both the key and the value must be unique within the map.
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Throws:
      IllegalArgumentException - if the key or the value is already present in the map
    • get

      V get(K key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the value to which the specified key is mapped, or null
    • getKeyByValue

      K getKeyByValue(V value)
      Returns the key associated with the specified value, or null if this map contains no mapping for the value.
      Parameters:
      value - the value whose associated key is to be returned
      Returns:
      the key associated with the specified value, or null
    • containsKey

      boolean containsKey(K key)
      Returns true if this map contains a mapping for the specified key.
      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

      boolean containsValue(V value)
      Returns true if this map maps one or more keys to the specified value.
      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

      void removeByKey(K key)
      Removes the mapping for a key from this map if it is present. Also removes the corresponding value from the bidirectional mapping.
      Parameters:
      key - the key whose mapping is to be removed from the map
    • removeByValue

      void removeByValue(V value)
      Removes the mapping for a value from this map if it is present. Also removes the corresponding key from the bidirectional mapping.
      Parameters:
      value - the value whose mapping is to be removed from the map
    • size

      int size()
      Returns the number of key-value mappings in this map.
      Returns:
      the number of key-value mappings in this map
    • getAll

      @Unmodifiable Map<K,V> getAll()
      Returns an unmodifiable Map view of the mappings contained in this map.
      Returns:
      an unmodifiable map containing all key-value pairs
    • of

      @SafeVarargs @ReleasedSince("26.10.0") static <R1, R2> UniqueMap<R1,R2> of(UniqueMap.Entry<R1,R2>... entries)
      Returns an unmodifiable UniqueMap containing the given key-value mappings.
      Type Parameters:
      R1 - the key type
      R2 - the value type
      Parameters:
      entries - the key-value pairs to populate the map with
      Returns:
      an immutable UniqueMap containing the specified entries
      Throws:
      NullPointerException - if the array or any entry/key/value is null
      IllegalArgumentException - if duplicate keys or duplicate values are provided