Interface UniqueMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- 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 ClassesModifier and TypeInterfaceDescriptionstatic final recordA simple immutable record representing a key-value pair. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(K key) Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(V value) Returnstrueif this map maps one or more keys to the specified value.Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.getAll()Returns an unmodifiableMapview of the mappings contained in this map.getKeyByValue(V value) Returns the key associated with the specified value, ornullif this map contains no mapping for the value.static <R1,R2> UniqueMap <R1, R2> of(UniqueMap.Entry<R1, R2>... entries) Returns an unmodifiableUniqueMapcontaining the given key-value mappings.voidAssociates the specified value with the specified key in this map.voidremoveByKey(K key) Removes the mapping for a key from this map if it is present.voidremoveByValue(V value) Removes the mapping for a value from this map if it is present.intsize()Returns the number of key-value mappings in this map.
-
Method Details
-
putUnique
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 associatedvalue- the value to be associated with the specified key- Throws:
IllegalArgumentException- if the key or the value is already present in the map
-
get
Returns the value to which the specified key is mapped, ornullif 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
Returns the key associated with the specified value, ornullif 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
Returnstrueif this map contains a mapping for the specified key.- Parameters:
key- the key whose presence in this map is to be tested- Returns:
trueif this map contains a mapping for the specified key
-
containsValue
Returnstrueif 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:
trueif this map maps one or more keys to the specified value
-
removeByKey
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
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
Returns an unmodifiableMapview 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 unmodifiableUniqueMapcontaining the given key-value mappings.- Type Parameters:
R1- the key typeR2- the value type- Parameters:
entries- the key-value pairs to populate the map with- Returns:
- an immutable
UniqueMapcontaining the specified entries - Throws:
NullPointerException- if the array or any entry/key/value is nullIllegalArgumentException- if duplicate keys or duplicate values are provided
-