Class LRUCache<K,​V>

java.lang.Object
application.model.LRUCache<K,​V>
Type Parameters:
K - The key of the LRU cache
V - The values to be stored in the cache
All Implemented Interfaces:
java.lang.Iterable<K>, java.util.Iterator<K>

public class LRUCache<K,​V>
extends java.lang.Object
implements java.lang.Iterable<K>, java.util.Iterator<K>
Implements a least recently used cache with a hashmap and queue
  • Constructor Summary

    Constructors 
    Constructor Description
    LRUCache​(int capacity)
    Constructs an LRU cache with the specified capacity
  • Method Summary

    Modifier and Type Method Description
    boolean containsKey​(K key)
    Checks if the key can be found in the cache
    V get​(K key)
    Gets a value associated with a key
    boolean hasNext()  
    java.util.Iterator<K> iterator()  
    K next()  
    void put​(K key, V value)
    Sets the a value in the LRU cache
    void remove​(K key)
    Removes the a value from the LRU cache O(N) removal by the way
    void setCapacity​(int capacity)
    Sets the capacity of the LRU cache
    void setCheckValid​(java.util.function.Function<V,​java.lang.Boolean> checkValid)
    Checks if a given value is valid in the LRU cache
    void setDeleteFunc​(java.util.function.BiConsumer<K,​V> deleteFunc)
    Sets the function that should run when a value is deleted
    int size()
    Size of the LRU cache

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator

    Methods inherited from interface java.util.Iterator

    forEachRemaining, remove
  • Constructor Details

    • LRUCache

      public LRUCache​(int capacity)
      Constructs an LRU cache with the specified capacity
      Parameters:
      capacity - The capacity of the LRU cache
  • Method Details

    • put

      public void put​(K key, V value)
      Sets the a value in the LRU cache
      Parameters:
      key - Key to associate value with
      value - Value to be set
    • remove

      public void remove​(K key)
      Removes the a value from the LRU cache O(N) removal by the way
      Parameters:
      key - Key to associated with the value
    • get

      public V get​(K key)
      Gets a value associated with a key
      Parameters:
      key - The key that the value is associated with
      Returns:
      Returns the value
    • containsKey

      public boolean containsKey​(K key)
      Checks if the key can be found in the cache
      Parameters:
      key - The key to search for
      Returns:
      Returns true if the key is found, false otherwise
    • size

      public int size()
      Size of the LRU cache
      Returns:
      Returns the size of the LRU cache
    • setDeleteFunc

      public void setDeleteFunc​(java.util.function.BiConsumer<K,​V> deleteFunc)
      Sets the function that should run when a value is deleted
      Parameters:
      deleteFunc - The function to run
    • setCheckValid

      public void setCheckValid​(java.util.function.Function<V,​java.lang.Boolean> checkValid)
      Checks if a given value is valid in the LRU cache
      Parameters:
      checkValid - The function to run
    • setCapacity

      public void setCapacity​(int capacity)
      Sets the capacity of the LRU cache
      Parameters:
      capacity - Capacity of the LRU cache
    • iterator

      public java.util.Iterator<K> iterator()
      Specified by:
      iterator in interface java.lang.Iterable<K>
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface java.util.Iterator<K>
    • next

      public K next()
      Specified by:
      next in interface java.util.Iterator<K>