Class LRUCache

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map, Cache
    Direct Known Subclasses:
    AbstractCachingDictionary.ObservableCache

    public class LRUCache
    extends java.util.LinkedHashMap
    implements Cache
    A fixed-capacity Cache that stores the most recently used elements. Once the cache reaches capacity, the least recently used elements will be removed.
    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>
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int _capacity  
    • Constructor Summary

      Constructors 
      Constructor Description
      LRUCache​(int capacity)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getCapacity()
      Returns the maximum number of elements the cache can hold.
      int getSize()
      Returns the current size of the cache.
      boolean isFull()  
      protected boolean removeEldestEntry​(java.util.Map.Entry eldest)  
      int setCapacity​(int capacity)
      Set the maximum number of elements the cache can hold.
      • 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 int _capacity
    • Constructor Detail

      • LRUCache

        public LRUCache​(int capacity)
        Parameters:
        capacity - the maximum number of elements that can be contained in the cache.
    • Method Detail

      • isFull

        public boolean isFull()
      • removeEldestEntry

        protected boolean removeEldestEntry​(java.util.Map.Entry eldest)
        Overrides:
        removeEldestEntry in class java.util.LinkedHashMap
      • setCapacity

        public int setCapacity​(int capacity)
        Description copied from interface: Cache
        Set the maximum number of elements the cache can hold.
        Specified by:
        setCapacity in interface Cache
      • getCapacity

        public int getCapacity()
        Description copied from interface: Cache
        Returns the maximum number of elements the cache can hold.
        Specified by:
        getCapacity in interface Cache
      • getSize

        public int getSize()
        Description copied from interface: Cache
        Returns the current size of the cache.
        Specified by:
        getSize in interface Cache