Class UnsynchronizedSymmetricMap<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 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 of UnsynchronizedSymmetricMap.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:

    Two insertion modes are provided:

    Additional symmetric operations:

    Iteration order: this map makes no guarantee on the order of elements returned by keySet(), values(), and entrySet(). However, all three views iterate over the same internal key-indexed chain, so their iteration orders are mutually consistent — the i-th key in keySet() corresponds to the i-th value in values() and the i-th entry in entrySet(). This order may change after a resize.

    This implementation is not thread-safe. For concurrent access, use SynchronizedSymmetricMap.

    See Also:
    Serialized Form
    • Constructor Detail

      • UnsynchronizedSymmetricMap

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

        public UnsynchronizedSymmetricMap​(int initialCapacity)
        Creates a new UnsynchronizedSymmetricMap 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
      • UnsynchronizedSymmetricMap

        public UnsynchronizedSymmetricMap​(int initialCapacity,
                                          float loadFactor)
        Creates a new UnsynchronizedSymmetricMap 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)
        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
      • 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.

        Specified by:
        put in interface Map<K,​V>
        Overrides:
        put in class AbstractMap<K,​V>
        Parameters:
        key - the key
        value - the value
        Returns:
        the previous value associated with key, or null
      • 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:
        safePut in interface SymmetricMap<K,​V>
        Parameters:
        key - the key
        value - 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 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
      • 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.

        Because values in a bijective map are unique by definition, this method returns a Set<V> rather than a Collection<V>.

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