Class SynchronizedSymmetricMap<K,​V>

  • Type Parameters:
    K - the type of keys
    V - the type of values
    All Implemented Interfaces:
    SymmetricMap<K,​V>, Serializable, Map<K,​V>

    public class SynchronizedSymmetricMap<K,​V>
    extends Object
    implements SymmetricMap<K,​V>, Serializable
    A thread-safe implementation of SymmetricMap, backed by an UnsynchronizedSymmetricMap and protected by a ReentrantReadWriteLock.

    Multiple threads may read concurrently. Write operations are exclusive: a write blocks until all ongoing reads have completed, and blocks any new reads until the write is done.

    The iterators returned by the views of this map (keySet(), values(), entrySet()) are fail-safe: they operate on a snapshot of the map taken at the time of the call, under a read lock. Subsequent modifications to the map are not reflected in the snapshot.

    Memory note: snapshot-based iterators copy the entire entry set at the time of the call. Avoid iterating over very large maps in memory-constrained environments.

    See Also:
    Serialized Form
    • Constructor Detail

      • SynchronizedSymmetricMap

        public SynchronizedSymmetricMap()
        Creates a new SynchronizedSymmetricMap with the default initial capacity (16) and load factor (0.75).
      • SynchronizedSymmetricMap

        public SynchronizedSymmetricMap​(int initialCapacity)
        Creates a new SynchronizedSymmetricMap with the given initial capacity and the default load factor (0.75).
        Parameters:
        initialCapacity - the initial capacity; must be at least 1
        Throws:
        IllegalArgumentException - if initialCapacity is less than 1
      • SynchronizedSymmetricMap

        public SynchronizedSymmetricMap​(int initialCapacity,
                                        float loadFactor)
        Creates a new SynchronizedSymmetricMap with the given initial capacity and load factor.
        Parameters:
        initialCapacity - the initial capacity; must be at least 1
        loadFactor - the load factor; must be positive
        Throws:
        IllegalArgumentException - if initialCapacity is less than 1 or loadFactor is not positive
    • Method Detail

      • getKey

        public Optional<K> getKey​(Object value)
        Description copied from interface: SymmetricMap
        Returns the key associated with the given value, or empty if not found.
        Specified by:
        getKey in interface SymmetricMap<K,​V>
        Parameters:
        value - the value to look up
        Returns:
        an Optional containing the key associated with value, or empty if not found
      • safePut

        public void safePut​(K key,
                            V value)
        Description copied from interface: SymmetricMap
        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:
        safePut in interface SymmetricMap<K,​V>
        Parameters:
        key - the key
        value - the value
      • removeByValue

        public Optional<K> removeByValue​(Object value)
        Description copied from interface: SymmetricMap
        Removes the entry associated with the given value, if present. Symmetric to Map.remove(Object) which removes by key.
        Specified by:
        removeByValue in interface SymmetricMap<K,​V>
        Parameters:
        value - the value whose entry is to be removed
        Returns:
        an Optional containing the key that was associated with value, or empty if not found
      • inverse

        public SynchronizedSymmetricMap<V,​K> inverse()
        Returns an independent copy of this map with keys and values swapped, as a SynchronizedSymmetricMap. The copy is taken under a read lock to guarantee a consistent snapshot.
        Specified by:
        inverse in interface SymmetricMap<K,​V>
        Returns:
        a new SynchronizedSymmetricMap<V, K> with all entries inverted
      • values

        public Set<V> values()
        Returns a Set view of the values contained in this map.

        This method narrows the return type of Map.values() from Collection to Set, 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.

        The returned set is a snapshot-based view: its iterator operates on a copy of the map taken under a read lock at the time Set.iterator() is called.

        Memory note: each call to Set.iterator() copies the entire entry set.

        Specified by:
        values in interface Map<K,​V>
        Specified by:
        values in interface SymmetricMap<K,​V>
        Returns:
        a set view of the values contained in this map
      • size

        public int size()
        Specified by:
        size in interface Map<K,​V>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface Map<K,​V>
      • containsKey

        public boolean containsKey​(Object key)
        Specified by:
        containsKey in interface Map<K,​V>
      • get

        public V get​(Object key)
        Specified by:
        get in interface Map<K,​V>
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface Map<K,​V>
      • remove

        public V remove​(Object key)
        Specified by:
        remove in interface Map<K,​V>
      • putAll

        public void putAll​(Map<? extends K,​? extends V> m)
        Specified by:
        putAll in interface Map<K,​V>
      • clear

        public void clear()
        Specified by:
        clear in interface Map<K,​V>
      • replace

        public V replace​(K key,
                         V value)
        Specified by:
        replace in interface Map<K,​V>
      • replace

        public boolean replace​(K key,
                               V oldValue,
                               V newValue)
        Specified by:
        replace in interface Map<K,​V>
      • replaceAll

        public void replaceAll​(BiFunction<? super K,​? super V,​? extends V> function)
        Replaces each value with the result of the given function applied to its key and current value. This operation is fully atomic — the write lock is held for its entire duration.

        Maintains bijectivity: if the function produces a duplicate value, the conflicting entry is silently removed.

        Specified by:
        replaceAll in interface Map<K,​V>
        Parameters:
        function - the remapping function
      • merge

        public V merge​(K key,
                       V value,
                       BiFunction<? super V,​? super V,​? extends V> remappingFunction)
        Specified by:
        merge in interface Map<K,​V>
      • compute

        public V compute​(K key,
                         BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        Specified by:
        compute in interface Map<K,​V>
      • computeIfPresent

        public V computeIfPresent​(K key,
                                  BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        Specified by:
        computeIfPresent in interface Map<K,​V>
      • keySet

        public Set<K> keySet()
        Returns a Set view of the keys contained in this map.

        The returned set is a snapshot-based view: its iterator operates on a copy of the map taken under a read lock at the time Set.iterator() is called.

        Memory note: each call to Set.iterator() copies the entire entry set.

        Specified by:
        keySet in interface Map<K,​V>
      • entrySet

        public Set<Map.Entry<K,​V>> entrySet()
        Returns a Set view of the entries contained in this map.

        The returned set is a snapshot-based view: its iterator operates on a copy of the map taken under a read lock at the time Set.iterator() is called.

        Memory note: each call to Set.iterator() copies the entire entry set.

        Specified by:
        entrySet in interface Map<K,​V>