Class PooledHashMap<K,​V>

  • Type Parameters:
    K - The map key type
    V - The map value type
    All Implemented Interfaces:
    java.util.Map<K,​V>

    public final class PooledHashMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    A bucket-based hash map with an internal re-usable map entry objects pool

    The goal of this map is to minimize memory allocation, leading to reduced time spent in garbage collection.

    This map avoids allocating a new map entry on each put operation by maintaining a pool of reusable (mutable) map entries and borrowing a map entry object from the pool to hold the given key-value of the put operation. The borrowed object is returned to the pool when the map entry key is removed from the map.

    This class is internal and is hence not for public use. Its APIs are unstable and can change at any time.

    This class is not thread-safe.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  PooledHashMap.Entry<K,​V>  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      void forEach​(java.util.function.BiConsumer<? super K,​? super V> action)  
      V get​(java.lang.Object key)
      Retrieves the mapped value for key.
      private int getBucket​(K key)  
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      V put​(K key, V value)
      Add a key, value pair to the map.
      void putAll​(java.util.Map<? extends K,​? extends V> m)  
      private void rehash()  
      V remove​(java.lang.Object key)
      Removes the mapping for the given key.
      int size()  
      java.util.Collection<V> values()  
      • 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, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • PooledHashMap

        public PooledHashMap​(int capacity)
        Creates a PooledHashMap with capacity buckets.

        The hashmap contains an array of buckets, each is an array-list of items. The number of buckets expands over time to avoid having too many items in one bucket, otherwise accessing an item by key won't be a constant time complexity.

        Parameters:
        capacity - The initial number of buckets to start with
    • Method Detail

      • put

        @Nullable
        public V put​(K key,
                     V value)
        Add a key, value pair to the map.

        Internally it uses a MapEntry from a pool of entries, to store this mapping

        Specified by:
        put in interface java.util.Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        Null if the was no previous mapping for this key, or the value of the previous mapping of this key
      • rehash

        private void rehash()
      • get

        @Nullable
        public V get​(java.lang.Object key)
        Retrieves the mapped value for key.
        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        The mapped value for key or null if there is no such mapping
      • remove

        @Nullable
        public V remove​(java.lang.Object key)
        Removes the mapping for the given key.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the map
        Returns:
        The value mapped to this key, if the mapping exists, or null otherwise
      • 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>
      • containsKey

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

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • clear

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

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

        private int getBucket​(K key)
      • entrySet

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

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

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

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