Class CopyOnWriteIdentityMap<K,​V>

  • All Implemented Interfaces:
    java.util.Map<K,​V>

    public class CopyOnWriteIdentityMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    A copy-on-write identity map. Write operations result in copying the underlying data so that simultaneous read operations are not affected. This allows for safe, unsynchronized traversal.

    Note: This class uses identity for key and value comparison, not equals.

    Since:
    3.5
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Remove all entries from the map.
      boolean containsKey​(java.lang.Object key)
      Check if the map contains the specified key.
      boolean containsValue​(java.lang.Object value)
      Check if the map contains the specified value.
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Returns a snapshot of the entries in this map.
      V get​(java.lang.Object key)
      Return the value object for the specified key.
      boolean isEmpty()
      Is the map empty?
      java.util.Set<K> keySet()
      Returns a snapshot of the keys in this map.
      V put​(K key, V value)
      Add a key, value pair to the map.
      void putAll​(java.util.Map<? extends K,​? extends V> source)
      Add all the entries from the specified map to this map.
      <L extends K>
      void
      putAll​(L[] keys)
      Add all the keys from the specified array to this map with the value null.
      V remove​(java.lang.Object key)
      Remove a key from the map and returns the value associated with the key.
      int size()
      Return the number of entries in the map.
      java.util.Collection<V> values()
      Returns a snapshot of the values in this map.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • CopyOnWriteIdentityMap

        public CopyOnWriteIdentityMap()
        Creates an empty map.
      • CopyOnWriteIdentityMap

        public CopyOnWriteIdentityMap​(CopyOnWriteIdentityMap<? extends K,​? extends V> source)
        Copy constructor.
        Parameters:
        source - The CopyOnWriteMap to copy.
    • Method Detail

      • put

        public V put​(K key,
                     V value)
        Add a key, value pair to the map. If the key object is already in the map, then its value is replaced with the new value. Keys are compared using identity.
        Specified by:
        put in interface java.util.Map<K,​V>
        Parameters:
        key - The key object to be added to the list.
        value - The value object to be associated with the key. This may be null.
        Returns:
        null if the specified key was newly added to the map. Otherwise the previous value of the key.
        Throws:
        java.lang.NullPointerException - If key is null.
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> source)
        Add all the entries from the specified map to this map.
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Parameters:
        source - The map whose entries are to be added to this map.
      • putAll

        public <L extends K> void putAll​(L[] keys)
        Add all the keys from the specified array to this map with the value null.
        Parameters:
        keys - The array of keys to be added to this map.
      • remove

        public V remove​(java.lang.Object key)
        Remove a key from the map and returns the value associated with the key. Key objects are compared using identity.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key - The key object to be removed from the map.
        Returns:
        null if the key was not in the list. Otherwise, the value associated with the key.
        Throws:
        java.lang.NullPointerException - If key is null.
      • clear

        public void clear()
        Remove all entries from the map.
        Specified by:
        clear in interface java.util.Map<K,​V>
      • isEmpty

        public boolean isEmpty()
        Is the map empty?
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Returns:
        true if the list is empty.
      • size

        public int size()
        Return the number of entries in the map.
        Specified by:
        size in interface java.util.Map<K,​V>
        Returns:
        The number of entries in the map.
      • get

        public V get​(java.lang.Object key)
        Return the value object for the specified key. Keys are compared using identity.
        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key - The key object.
        Returns:
        The value object for the specified key.
        Throws:
        java.lang.NullPointerException - If key is null.
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Check if the map contains the specified key. Keys are compared using identity.
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Parameters:
        key - The key object.
        Returns:
        true if the specified key is in the map.
        Throws:
        java.lang.NullPointerException - If key is null.
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Check if the map contains the specified value. Values are compared using identity.
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Parameters:
        value - The value object.
        Returns:
        true if the specified value is in the map.
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Returns a snapshot of the entries in this map. Changes to the returned set or this map will not affect each other.
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        A Set of Map.Entry for each entry in this map. The entries returned by the set cannot be modified.
      • keySet

        public java.util.Set<K> keySet()
        Returns a snapshot of the keys in this map. Changes to the returned set or this map will not affect each other.
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Returns:
        A Set of the key objects in this map
      • values

        public java.util.Collection<V> values()
        Returns a snapshot of the values in this map. Changes to the returned set or this map will not affect each other.
        Specified by:
        values in interface java.util.Map<K,​V>
        Returns:
        A Collection of the value objects in this map.