Package application.model
Class LRUCache<K,V>
java.lang.Object
application.model.LRUCache<K,V>
- Type Parameters:
K- The key of the LRU cacheV- 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 booleancontainsKey(K key)Checks if the key can be found in the cacheVget(K key)Gets a value associated with a keybooleanhasNext()java.util.Iterator<K>iterator()Knext()voidput(K key, V value)Sets the a value in the LRU cachevoidremove(K key)Removes the a value from the LRU cache O(N) removal by the wayvoidsetCapacity(int capacity)Sets the capacity of the LRU cachevoidsetCheckValid(java.util.function.Function<V,java.lang.Boolean> checkValid)Checks if a given value is valid in the LRU cachevoidsetDeleteFunc(java.util.function.BiConsumer<K,V> deleteFunc)Sets the function that should run when a value is deletedintsize()Size of the LRU cache
-
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
Sets the a value in the LRU cache- Parameters:
key- Key to associate value withvalue- Value to be set
-
remove
Removes the a value from the LRU cache O(N) removal by the way- Parameters:
key- Key to associated with the value
-
get
Gets a value associated with a key- Parameters:
key- The key that the value is associated with- Returns:
- Returns the value
-
containsKey
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
Sets the function that should run when a value is deleted- Parameters:
deleteFunc- The function to run
-
setCheckValid
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
- Specified by:
iteratorin interfacejava.lang.Iterable<K>
-
hasNext
public boolean hasNext()- Specified by:
hasNextin interfacejava.util.Iterator<K>
-
next
- Specified by:
nextin interfacejava.util.Iterator<K>
-