-
- Type Parameters:
K- the type of keysV- the type of values
- All Superinterfaces:
Map<K,V>
- All Known Implementing Classes:
SynchronizedSymmetricMap,UnsynchronizedSymmetricMap
public interface SymmetricMap<K,V> extends Map<K,V>
A bijective map where both keys and values are unique.In addition to the standard
Mapcontract, this interface exposes symmetric operations — lookup, removal, and insertion — that work equally in both directions:Map.get(Object)— retrieve a value by key (inherited fromMap)getKey(Object)— retrieve a key by valueMap.remove(Object)— remove an entry by key (inherited fromMap)removeByValue(Object)— remove an entry by valueMap.put(Object, Object)— insert permissively (inherited fromMap)safePut(Object, Object)— insert strictly
Because values are unique by definition,
values()returns aSet<V>rather than aCollection<V>.Two implementations are provided:
UnsynchronizedSymmetricMap— not thread-safe.SynchronizedSymmetricMap— thread-safe, backed by aReentrantReadWriteLock.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Optional<K>getKey(Object value)Returns the key associated with the given value, or empty if not found.SymmetricMap<V,K>inverse()Returns an independent copy of this map with keys and values swapped.Optional<K>removeByValue(Object value)Removes the entry associated with the given value, if present.voidsafePut(K key, V value)Associates the given key with the given value, throwing an exception if the key or value already exists in this map.Set<V>values()Returns aSetview of the values contained in this map.-
Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size
-
-
-
-
Method Detail
-
getKey
Optional<K> getKey(Object value)
Returns the key associated with the given value, or empty if not found.- Parameters:
value- the value to look up- Returns:
- an
Optionalcontaining the key associated withvalue, or empty if not found
-
safePut
void safePut(K key, V value)
Associates the given key with the given value, throwing an exception if the key or value already exists in this map.Use
Map.put(Object, Object)for a permissive insertion that silently removes conflicting entries.- Parameters:
key- the keyvalue- the value- Throws:
IllegalArgumentException- if the key or value already exists
-
removeByValue
Optional<K> removeByValue(Object value)
Removes the entry associated with the given value, if present. Symmetric toMap.remove(Object)which removes by key.- Parameters:
value- the value whose entry is to be removed- Returns:
- an
Optionalcontaining the key that was associated withvalue, or empty if not found
-
inverse
SymmetricMap<V,K> inverse()
Returns an independent copy of this map with keys and values swapped. Modifications to the returned map do not affect this map, and vice versa.The returned map is of the same type as this map: an
UnsynchronizedSymmetricMapreturns anUnsynchronizedSymmetricMap, and aSynchronizedSymmetricMapreturns aSynchronizedSymmetricMap.- Returns:
- a new
SymmetricMap<V, K>with all entries inverted
-
values
Set<V> values()
Returns aSetview of the values contained in this map.This method narrows the return type of
Map.values()fromCollectiontoSet, which is valid since values in a bijective map are unique by definition.The returned set is a live view of the map: changes to the map are reflected in the set, and vice versa.
-
-