public class SimpleLRUCache
extends java.util.HashMap
HashMap with a maximum size and an aggregated List
as LRU queue.| Modifier and Type | Field and Description |
|---|---|
private java.util.LinkedList |
list
LRU list.
|
private int |
maxCacheSize
Maximum cache size.
|
| Constructor and Description |
|---|
SimpleLRUCache(int maxCacheSize)
Constructs a new LRU cache instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Overrides clear() to also clear the LRU list.
|
private void |
freshenKey(java.lang.Object key)
Moves the specified value to the top of the LRU list (the bottom of the
list is where least recently used items live).
|
java.lang.Object |
get(java.lang.Object key)
Overrides
get() so that it also updates the LRU list. |
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Overrides
put() so that it also updates the LRU list. |
java.lang.Object |
remove(java.lang.Object key) |
clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll, size, valuesprivate final int maxCacheSize
private final java.util.LinkedList list
public SimpleLRUCache(int maxCacheSize)
maxCacheSize - the maximum number of entries in this cache before
entries are aged offpublic void clear()
clear in interface java.util.Mapclear in class java.util.HashMappublic java.lang.Object put(java.lang.Object key,
java.lang.Object value)
put() so that it also updates the LRU list.put in interface java.util.Mapput in class java.util.HashMapkey - key with which the specified value is to be associatedvalue - value to be associated with the keynull if there
was no mapping for key; a null return can also
indicate that the cache previously associated null
with the specified keyMap.put(Object, Object)public java.lang.Object get(java.lang.Object key)
get() so that it also updates the LRU list.get in interface java.util.Mapget in class java.util.HashMapkey - key with which the expected value is associatednull if the map contains no mapping for this keypublic java.lang.Object remove(java.lang.Object key)
remove in interface java.util.Mapremove in class java.util.HashMapMap.remove(Object)private void freshenKey(java.lang.Object key)
key - key of the value to move to the top of the listGenerated on December 23 2017