Class LRU<K,​V>

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

    public class LRU<K,​V>
    extends java.util.LinkedHashMap<K,​V>
    A least recently used map.
    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>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected ILRUEntryCleaner cleaner
      The entry cleaner (for performing cleanup code on removed entries).
      protected int max
      The maximum number of entries.
    • Constructor Summary

      Constructors 
      Constructor Description
      LRU()
      Create a new LRU with 1000 entries.
      LRU​(int max)
      Create a new LRU.
      LRU​(int max, ILRUEntryCleaner cleaner)
      Create a new LRU.
      LRU​(int max, ILRUEntryCleaner cleaner, boolean accessorder)
      Create a new LRU.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ILRUEntryCleaner getCleaner()
      Get the cleaner object.
      int getMaxEntries()
      Get the maximum number of the
      V put​(K key, V value)  
      boolean removeEldestEntry​(java.util.Map.Entry<K,​V> eldest)
      Remove the eldest entry.
      void setCleaner​(ILRUEntryCleaner cleaner)
      Set the cleaner object.
      void setMaxEntries​(int max)
      Set the maximum number of entries.
      • 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, 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, putAll, putIfAbsent, remove, remove, replace, replace, size
    • Field Detail

      • max

        protected int max
        The maximum number of entries.
      • cleaner

        protected ILRUEntryCleaner cleaner
        The entry cleaner (for performing cleanup code on removed entries).
    • Constructor Detail

      • LRU

        public LRU()
        Create a new LRU with 1000 entries.
      • LRU

        public LRU​(int max)
        Create a new LRU.
        Parameters:
        max - The maximum number of entries.
      • LRU

        public LRU​(int max,
                   ILRUEntryCleaner cleaner)
        Create a new LRU.
        Parameters:
        max - The maximum number of entries.
      • LRU

        public LRU​(int max,
                   ILRUEntryCleaner cleaner,
                   boolean accessorder)
        Create a new LRU.
        Parameters:
        max - The maximum number of entries.
        cleaner - Optional cleaner.
        accessorder - If true, entry use is refreshed on read as well as write.
    • Method Detail

      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.HashMap<K,​V>
      • getMaxEntries

        public int getMaxEntries()
        Get the maximum number of the
        Returns:
        The maximum number of entries.
      • setMaxEntries

        public void setMaxEntries​(int max)
        Set the maximum number of entries.
        Parameters:
        max - The maximum number of entries.
      • getCleaner

        public ILRUEntryCleaner getCleaner()
        Get the cleaner object.
        Returns:
        The cleaner object.
      • setCleaner

        public void setCleaner​(ILRUEntryCleaner cleaner)
        Set the cleaner object.
        Parameters:
        cleaner - The cleaner object.
      • removeEldestEntry

        public boolean removeEldestEntry​(java.util.Map.Entry<K,​V> eldest)
        Remove the eldest entry.
        Overrides:
        removeEldestEntry in class java.util.LinkedHashMap<K,​V>
        Parameters:
        eldest - The eldest entry.
        Returns:
        True if need to be removed.