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 boolean
containsKey(K key)
Checks if the key can be found in the cacheV
get(K key)
Gets a value associated with a keyboolean
hasNext()
java.util.Iterator<K>
iterator()
K
next()
void
put(K key, V value)
Sets the a value in the LRU cachevoid
remove(K key)
Removes the a value from the LRU cache O(N) removal by the wayvoid
setCapacity(int capacity)
Sets the capacity of the LRU cachevoid
setCheckValid(java.util.function.Function<V,java.lang.Boolean> checkValid)
Checks if a given value is valid in the LRU cachevoid
setDeleteFunc(java.util.function.BiConsumer<K,V> deleteFunc)
Sets the function that should run when a value is deletedint
size()
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:
iterator
in interfacejava.lang.Iterable<K>
-
hasNext
public boolean hasNext()- Specified by:
hasNext
in interfacejava.util.Iterator<K>
-
next
- Specified by:
next
in interfacejava.util.Iterator<K>
-