public class LRUCache extends LinkedHashMap implements Cache
Cache
that stores the most recently used elements. Once the cache reaches
capacity, the least recently used elements will be removed.AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
LRUCache(int capacity) |
Modifier and Type | Method and 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(Map.Entry eldest) |
int |
setCapacity(int capacity)
Set the maximum number of elements the cache can hold.
|
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, values
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
equals, hashCode, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
public LRUCache(int capacity)
capacity
- the maximum number of elements that can be contained in the cache.public boolean isFull()
protected boolean removeEldestEntry(Map.Entry eldest)
removeEldestEntry
in class LinkedHashMap
public int setCapacity(int capacity)
Cache
setCapacity
in interface Cache
public int getCapacity()
Cache
getCapacity
in interface Cache
Copyright © 2024. All rights reserved.