Class FastCopyHashMap<K,​V>

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

    public class FastCopyHashMap<K,​V>
    extends java.util.AbstractMap<K,​V>
    implements java.util.Map<K,​V>, java.lang.Cloneable, java.io.Serializable
    A HashMap that is optimized for fast shallow copies. If the copy-ctor is passed another FastCopyHashMap, or clone is called on this map, the shallow copy can be performed using little more than a single array copy. In order to accomplish this, immutable objects must be used internally, so update operations result in slightly more object churn than HashMap. Note: It is very important to use a smaller load factor than you normally would for HashMap, since the implementation is open-addressed with linear probing. With a 50% load-factor a get is expected to return in only 2 probes. However, a 90% load-factor is expected to return in around 50 probes.
    See Also:
    Serialized Form
    • Field Detail

      • NULL

        private static final java.lang.Object NULL
        Marks null keys.
      • serialVersionUID

        private static final long serialVersionUID
        Serialization ID
        See Also:
        Constant Field Values
      • DEFAULT_CAPACITY

        private static final int DEFAULT_CAPACITY
        Same default as HashMap, must be a power of 2
        See Also:
        Constant Field Values
      • MAXIMUM_CAPACITY

        private static final int MAXIMUM_CAPACITY
        MAX_INT - 1
        See Also:
        Constant Field Values
      • DEFAULT_LOAD_FACTOR

        private static final float DEFAULT_LOAD_FACTOR
        67%, just like IdentityHashMap
        See Also:
        Constant Field Values
      • size

        private transient int size
        The current number of key-value pairs
      • threshold

        private transient int threshold
        The next resize
      • loadFactor

        private final float loadFactor
        The user defined load factor which defines when to resize
      • modCount

        private transient int modCount
        Counter used to detect changes made outside of an iterator
    • Constructor Detail

      • FastCopyHashMap

        public FastCopyHashMap​(int initialCapacity,
                               float loadFactor)
      • FastCopyHashMap

        public FastCopyHashMap​(java.util.Map<? extends K,​? extends V> map)
      • FastCopyHashMap

        public FastCopyHashMap​(int initialCapacity)
      • FastCopyHashMap

        public FastCopyHashMap()
    • Method Detail

      • init

        private void init​(int initialCapacity,
                          float loadFactor)
      • hash

        private static final int hash​(java.lang.Object key)
      • maskNull

        private static final <K> K maskNull​(K key)
      • unmaskNull

        private static final <K> K unmaskNull​(K key)
      • nextIndex

        private int nextIndex​(int index,
                              int length)
      • eq

        private static final boolean eq​(java.lang.Object o1,
                                        java.lang.Object o2)
      • index

        private static final int index​(int hashCode,
                                       int length)
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<K,​V>
        Overrides:
        size in class java.util.AbstractMap<K,​V>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Overrides:
        isEmpty in class java.util.AbstractMap<K,​V>
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.AbstractMap<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Overrides:
        containsKey in class java.util.AbstractMap<K,​V>
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Overrides:
        containsValue 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>
      • resize

        private void resize​(int from)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> map)
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Overrides:
        putAll in class java.util.AbstractMap<K,​V>
      • remove

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

        private void relocate​(int start)
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.AbstractMap<K,​V>
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.util.AbstractMap<K,​V>
      • printDebugStats

        public void printDebugStats()
      • 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>
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Overrides:
        keySet in class java.util.AbstractMap<K,​V>
      • values

        public java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
        Overrides:
        values in class java.util.AbstractMap<K,​V>
      • readObject

        private void readObject​(java.io.ObjectInputStream s)
                         throws java.io.IOException,
                                java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • putForCreate

        private void putForCreate​(K key,
                                  V value)
      • writeObject

        private void writeObject​(java.io.ObjectOutputStream s)
                          throws java.io.IOException
        Throws:
        java.io.IOException