Class BoundedHashMap<K,​V>

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

    public class BoundedHashMap<K,​V>
    extends java.util.LinkedHashMap<K,​V>
    BoundedHashMap is a map with a fixed capacity. When the map's size exceeds its capacity, it automatically removes elements until its size is equal to its capacity.

    BoundedHashMap can operate in two ordering modes: insertion order and access order. The mode specified which entries are automatically removed when the map is over capacity. In insertion order mode the map removes the eldest entry (the first entry added). In access order mode, the map automatically removes the least recently used entry.

    See Also:
    Serialized Form
    • 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>
    • Constructor Summary

      Constructors 
      Constructor Description
      BoundedHashMap()
      Creates a BoundedHashMap with a capacity of 16, in insertion order mode.
      BoundedHashMap​(int capacity)
      Creates a BoundedHashMap with a specified maximum capacity, in insertion order mode.
      BoundedHashMap​(int capacity, boolean accessOrder)
      Creates a BoundedHashMap with a specified maximum capacity and ordering mode.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getCapacity()
      Returns the maximum number of entries in the map.
      protected static int getInitialCapacity​(int capacity, float loadFactor)  
      protected boolean removeEldestEntry​(java.util.Map.Entry<K,​V> eldest)  
      protected void removeOverCapacityEntries()
      Removes the first n entries in the map, where n is the number of entries in the map beyond its capacity.
      void setCapacity​(int capacity)
      Sets the maximum number of entries in the map.
      • Methods inherited from class java.util.LinkedHashMap

        clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, values
      • Methods inherited from class java.util.HashMap

        clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
      • Methods inherited from class java.util.AbstractMap

        equals, hashCode, toString
      • Methods inherited from class java.lang.Object

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

        compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
    • Constructor Detail

      • BoundedHashMap

        public BoundedHashMap​(int capacity,
                              boolean accessOrder)
        Creates a BoundedHashMap with a specified maximum capacity and ordering mode.
        Parameters:
        capacity - the maximum number of entries in the map.
        accessOrder - the ordering mode: true specifies access order, false specifies insertion order.
      • BoundedHashMap

        public BoundedHashMap​(int capacity)
        Creates a BoundedHashMap with a specified maximum capacity, in insertion order mode.
        Parameters:
        capacity - the maximum number of entries in the map.
      • BoundedHashMap

        public BoundedHashMap()
        Creates a BoundedHashMap with a capacity of 16, in insertion order mode.
    • Method Detail

      • getCapacity

        public int getCapacity()
        Returns the maximum number of entries in the map.
        Returns:
        maximum number of entries in the map.
      • setCapacity

        public void setCapacity​(int capacity)
        Sets the maximum number of entries in the map. If the new capacity is less than the map's current size, this automatically removes entries until the map's size is equal to its capacity.
        Parameters:
        capacity - maximum number of entries in the map.
      • getInitialCapacity

        protected static int getInitialCapacity​(int capacity,
                                                float loadFactor)
      • removeEldestEntry

        protected boolean removeEldestEntry​(java.util.Map.Entry<K,​V> eldest)
        Overrides:
        removeEldestEntry in class java.util.LinkedHashMap<K,​V>
      • removeOverCapacityEntries

        protected void removeOverCapacityEntries()
        Removes the first n entries in the map, where n is the number of entries in the map beyond its capacity. Note that the entry set and the corresponding iterator are backed by the map itself, so changes to an entry iterator correspond to changes in the map. We use the iterator's remove() method because we're removing elements from the entry set as we iterate over them.