Class Object2ObjectHashMap<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this map.
    V - the type of mapped values.
    All Implemented Interfaces:
    java.util.Map<K,​V>
    Direct Known Subclasses:
    Object2NullableObjectHashMap

    public class Object2ObjectHashMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    An open-addressing with linear probing hash map, same algorithm as Int2IntHashMap.
    • Constructor Detail

      • Object2ObjectHashMap

        public Object2ObjectHashMap​(int initialCapacity,
                                    float loadFactor)
        Create a map with initial capacity and load factor.
        Parameters:
        initialCapacity - for the map to override MIN_CAPACITY
        loadFactor - for the map to override Hashing.DEFAULT_LOAD_FACTOR.
      • Object2ObjectHashMap

        public Object2ObjectHashMap​(int initialCapacity,
                                    float loadFactor,
                                    boolean shouldAvoidAllocation)
        Create a new instance with specified parameters.
        Parameters:
        initialCapacity - for the map to override MIN_CAPACITY
        loadFactor - for the map to override Hashing.DEFAULT_LOAD_FACTOR.
        shouldAvoidAllocation - should allocation be avoided by caching iterators and map entries.
      • Object2ObjectHashMap

        public Object2ObjectHashMap​(Object2ObjectHashMap<K,​V> mapToCopy)
        Copy construct a new map from an existing one.
        Parameters:
        mapToCopy - for construction.
    • Method Detail

      • loadFactor

        public float loadFactor()
        Get the load factor applied for resize operations.
        Returns:
        the load factor applied for resize operations.
      • resizeThreshold

        public int resizeThreshold()
        Get the actual threshold which when reached the map will resize. This is a function of the current capacity and load factor.
        Returns:
        the threshold when the map will resize.
      • capacity

        public int capacity()
        Get the total capacity for the map to which the load factor will be a fraction of.
        Returns:
        the total capacity for the map.
      • size

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

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

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

        private V getMapped​(java.lang.Object key)
      • put

        public V put​(K key,
                     V value)
        Put a key value pair into the map.
        Specified by:
        put in interface java.util.Map<K,​V>
        Parameters:
        key - lookup key
        value - new value, must not be null
        Returns:
        current value associated with key, or null if none found
        Throws:
        java.lang.IllegalArgumentException - if value is null
      • increaseCapacity

        private void increaseCapacity()
      • rehash

        private void rehash​(int newCapacity)
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Does the map contain the value.
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Parameters:
        value - to be tested against contained values.
        Returns:
        true if contained otherwise false.
      • clear

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

        public void compact()
        Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
      • forEach

        public void forEach​(java.util.function.BiConsumer<? super K,​? super V> consumer)
        Specified by:
        forEach in interface java.util.Map<K,​V>
      • containsKey

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

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

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

        private void compactChain​(int deleteKeyIndex)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
      • computeIfAbsent

        public V computeIfAbsent​(K key,
                                 java.util.function.Function<? super K,​? extends V> mappingFunction)
        Specified by:
        computeIfAbsent in interface java.util.Map<K,​V>
      • computeIfPresent

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

        public V compute​(K key,
                         java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        Specified by:
        compute in interface java.util.Map<K,​V>
      • mapNullValue

        protected java.lang.Object mapNullValue​(java.lang.Object value)
        Handle incoming null value and optionally replace with another non-null counterpart.
        Parameters:
        value - value to be handled.
        Returns:
        replacement value.
      • unmapNullValue

        protected V unmapNullValue​(java.lang.Object value)
        Handle incoming non-null value and optionally replace it with the null value counterpart. This is the opposite of the mapNullValue(Object) method.
        Parameters:
        value - value to be handled.
        Returns:
        replacement value.
        See Also:
        mapNullValue(Object)
      • next

        private static int next​(int index,
                                int mask)
      • capacity

        private void capacity​(int newCapacity)