Class LeastRecentlyUsedMap<K,​V>

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

    public class LeastRecentlyUsedMap<K,​V>
    extends java.util.LinkedHashMap<K,​V>
    The LeastRecentlyUsedMap is a hash map that keeps only those entries most recently used. This acts much like a hot spot cache for specific keys that are used frequently. It also allows for algorithms to keep hot spot values available in the cache without the risk of running out of memory.
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  LeastRecentlyUsedMap.RemovalListener<K,​V>
      The RemovalListener is used with the least recently used hash map to listen for removals.
      • 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>
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean removeEldestEntry​(java.util.Map.Entry<K,​V> eldest)
      This is used to determine if an entry should be removed from the cache.
      • 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

        finalize, 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
    • Field Detail

      • capacity

        private final int capacity
        This is the number of items to keep within the cache.
    • Constructor Detail

      • LeastRecentlyUsedMap

        public LeastRecentlyUsedMap()
        Constructor for the LeastRecentlyUsedMap object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.
      • LeastRecentlyUsedMap

        public LeastRecentlyUsedMap​(int capacity)
        Constructor for the LeastRecentlyUsedMap object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.
        Parameters:
        capacity - this is the capacity of the hash container
      • LeastRecentlyUsedMap

        public LeastRecentlyUsedMap​(LeastRecentlyUsedMap.RemovalListener<K,​V> listener)
        Constructor for the LeastRecentlyUsedMap object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.
        Parameters:
        listener - this listens for entries that are removed
      • LeastRecentlyUsedMap

        public LeastRecentlyUsedMap​(LeastRecentlyUsedMap.RemovalListener<K,​V> listener,
                                    int capacity)
        Constructor for the LeastRecentlyUsedMap object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.
        Parameters:
        listener - this listens for entries that are removed
        capacity - this is the capacity of the hash container
    • Method Detail

      • removeEldestEntry

        protected boolean removeEldestEntry​(java.util.Map.Entry<K,​V> eldest)
        This is used to determine if an entry should be removed from the cache. If the cache has reached its capacity then the listener, if one was specified is given a callback to tell any other participating objects the entry has been removed.
        Overrides:
        removeEldestEntry in class java.util.LinkedHashMap<K,​V>
        Parameters:
        eldest - this is the candidate for removal