- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- fr.dufrenoy.util.UnsynchronizedSymmetricMap<K,V>
-
- Type Parameters:
K- the type of keysV- the type of values
- All Implemented Interfaces:
SymmetricMap<K,V>,Serializable,Map<K,V>
public class UnsynchronizedSymmetricMap<K,V> extends AbstractMap<K,V> implements SymmetricMap<K,V>, Serializable
A bijective map where both keys and values are unique, backed by a single array ofUnsynchronizedSymmetricMap.Buckets with two independent collision chains per bucket — one indexed by key hash, one indexed by value hash.This allows O(1) average lookup in both directions:
get(Object)— retrieve a value by keygetKey(Object)— retrieve a key by value
Two insertion modes are provided:
put(Object, Object)— always overwrites; if the key or value already exists, the conflicting entry is silently removed.safePut(Object, Object)— throwsIllegalArgumentExceptionif the key or value already exists.
Additional symmetric operations:
removeByValue(Object)— remove an entry by value, symmetric toremove(Object)which removes by key.inverse()— return an independent copy of this map with keys and values swapped, as anUnsynchronizedSymmetricMap.
Iteration order: this map makes no guarantee on the order of elements returned by
keySet(),values(), andentrySet(). However, all three views iterate over the same internal key-indexed chain, so their iteration orders are mutually consistent — the i-th key inkeySet()corresponds to the i-th value invalues()and the i-th entry inentrySet(). This order may change after a resize.This implementation is not thread-safe. For concurrent access, use
SynchronizedSymmetricMap.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor Description UnsynchronizedSymmetricMap()Creates a newUnsynchronizedSymmetricMapwith the default initial capacity (16) and load factor (0.75).UnsynchronizedSymmetricMap(int initialCapacity)Creates a newUnsynchronizedSymmetricMapwith the given initial capacity and the default load factor (0.75).UnsynchronizedSymmetricMap(int initialCapacity, float loadFactor)Creates a newUnsynchronizedSymmetricMapwith the given initial capacity and load factor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()booleancontainsKey(Object key)booleancontainsValue(Object value)Set<Map.Entry<K,V>>entrySet()Vget(Object key)Optional<K>getKey(Object value)Returns the key associated with the given value, or empty if not found.UnsynchronizedSymmetricMap<V,K>inverse()Returns an independent copy of this map with keys and values swapped, as anUnsynchronizedSymmetricMap.Set<K>keySet()Vput(K key, V value)Associates the given key with the given value.Vremove(Object key)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.intsize()Set<V>values()Returns aSetview of the values contained in this map.-
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, isEmpty, putAll, toString
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Constructor Detail
-
UnsynchronizedSymmetricMap
public UnsynchronizedSymmetricMap()
Creates a newUnsynchronizedSymmetricMapwith the default initial capacity (16) and load factor (0.75).
-
UnsynchronizedSymmetricMap
public UnsynchronizedSymmetricMap(int initialCapacity)
Creates a newUnsynchronizedSymmetricMapwith the given initial capacity and the default load factor (0.75).- Parameters:
initialCapacity- the initial capacity; must be at least 1- Throws:
IllegalArgumentException- ifinitialCapacityis less than 1
-
UnsynchronizedSymmetricMap
public UnsynchronizedSymmetricMap(int initialCapacity, float loadFactor)Creates a newUnsynchronizedSymmetricMapwith the given initial capacity and load factor.- Parameters:
initialCapacity- the initial capacity; must be at least 1loadFactor- the load factor; must be positive- Throws:
IllegalArgumentException- ifinitialCapacityis less than 1 orloadFactoris not positive
-
-
Method Detail
-
size
public int size()
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<K,V>- Overrides:
containsKeyin classAbstractMap<K,V>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<K,V>- Overrides:
containsValuein classAbstractMap<K,V>
-
getKey
public Optional<K> getKey(Object value)
Returns the key associated with the given value, or empty if not found.- Specified by:
getKeyin interfaceSymmetricMap<K,V>- Parameters:
value- the value to look up- Returns:
- an
Optionalcontaining the key associated withvalue, or empty if not found
-
put
public V put(K key, V value)
Associates the given key with the given value. If the key or value already exists, the conflicting entry is silently removed to maintain bijectivity.If the exact same key-value pair already exists, this method is a no-op and returns the existing value.
-
safePut
public 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.- Specified by:
safePutin interfaceSymmetricMap<K,V>- Parameters:
key- the keyvalue- the value- Throws:
IllegalArgumentException- if the key or value already exists
-
removeByValue
public Optional<K> removeByValue(Object value)
Removes the entry associated with the given value, if present. Symmetric toMap.remove(Object)which removes by key.- Specified by:
removeByValuein interfaceSymmetricMap<K,V>- 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
public UnsynchronizedSymmetricMap<V,K> inverse()
Returns an independent copy of this map with keys and values swapped, as anUnsynchronizedSymmetricMap. Modifications to the returned map do not affect this map, and vice versa.- Specified by:
inversein interfaceSymmetricMap<K,V>- Returns:
- a new
UnsynchronizedSymmetricMap<V, K>with all entries inverted
-
clear
public void clear()
-
values
public 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.
Because values in a bijective map are unique by definition, this method returns a
Set<V>rather than aCollection<V>.
-
-