Class CopyOnWriteHashMap<K,​V>

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

    public class CopyOnWriteHashMap<K,​V>
    extends java.util.AbstractMap<K,​V>
    implements java.lang.Cloneable
    A hashmap wrapper which defers cloning of the enclosed map until it is written. Use this to make clones cheap in maps which are often not further modified.

    As with regular maps, this can only be used safely if the content of the map is immutable. If not, the copyMap(java.util.Map<K, V>) method can be overridden to perform a deep clone.

    Author:
    bratseth
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      CopyOnWriteHashMap<K,​V> clone()  
      protected java.util.Map<K,​V> copyMap​(java.util.Map<K,​V> original)
      Make a copy of the given map with the requisite deepness.
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      V put​(K key, V value)  
      V remove​(java.lang.Object key)
      Override to avoid using iterator.remove
      • Methods inherited from class java.util.AbstractMap

        clear, containsKey, containsValue, equals, get, hashCode, isEmpty, keySet, putAll, size, toString, values
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

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

      • CopyOnWriteHashMap

        public CopyOnWriteHashMap()
      • CopyOnWriteHashMap

        public CopyOnWriteHashMap​(int capacity)
      • CopyOnWriteHashMap

        public CopyOnWriteHashMap​(java.util.Map<K,​V> map)
    • Method Detail

      • copyMap

        protected java.util.Map<K,​V> copyMap​(java.util.Map<K,​V> original)
        Make a copy of the given map with the requisite deepness. This default implementation does return new HashMap<>(original);
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Specified by:
        entrySet in class java.util.AbstractMap<K,​V>
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.AbstractMap<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Override to avoid using iterator.remove
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.AbstractMap<K,​V>