Class Object2ObjectHashMap<K,V>

java.lang.Object
org.agrona.collections.Object2ObjectHashMap<K,V>
All Implemented Interfaces:
Map<K,V>
Direct Known Subclasses:
Object2NullableObjectHashMap

public class Object2ObjectHashMap<K,V> extends Object implements Map<K,V>
An open-addressing with linear probing hash map, same algorithm as Int2IntHashMap.
  • Field Details

  • Constructor Details

    • Object2ObjectHashMap

      public Object2ObjectHashMap()
      Default constructor, i.e. create a map with MIN_CAPACITY and Hashing.DEFAULT_LOAD_FACTOR.
    • 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)
      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 Details

    • 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 Map<K,V>
    • isEmpty

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

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • getMapped

      private V getMapped(Object key)
    • put

      public V put(K key, V value)
      Put a key value pair into the map.
      Specified by:
      put in interface 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:
      IllegalArgumentException - if value is null
    • increaseCapacity

      private void increaseCapacity()
    • rehash

      private void rehash(int newCapacity)
    • containsValue

      public boolean containsValue(Object value)
      Does the map contain the value.
      Specified by:
      containsValue in interface 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 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(BiConsumer<? super K,? super V> consumer)
      Specified by:
      forEach in interface Map<K,V>
    • containsKey

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

      public void putAll(Map<? extends K,? extends V> map)
      Specified by:
      putAll in interface Map<K,V>
    • keySet

      public Object2ObjectHashMap<K,V>.KeySet keySet()
      Specified by:
      keySet in interface Map<K,V>
    • values

      Specified by:
      values in interface Map<K,V>
    • entrySet

      public Object2ObjectHashMap<K,V>.EntrySet entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • compactChain

      private void compactChain(int deleteKeyIndex)
    • toString

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

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

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

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

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

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

      protected Object mapNullValue(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(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:
    • next

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

      private void capacity(int newCapacity)