K - type of key stored in the Map.V - type of values stored in the Map.public final class LruCache<K,V>
extends java.util.LinkedHashMap<K,V>
LinkedHashMap.
The implementation is not thread safe and access must be synchronised in a multi threaded application. For example:
final int initialCapacity = 10;
final int maximumEntries = 20;
final Map<DateRange, Object> cache =
Collections.synchronizedMap(new LruCache(initialCapacity, maximumCapacity, 0.75F));
The LinkedHashMap orders its entries by access order so that once the size of the map reaches the
maximumEntries value, the least read entry is removed from the map when a new entry is added.
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(java.lang.Object object) |
static <K,V> LruCache<K,V> |
getCache(int initialCapacity,
int maximumEntries,
float loadFactor)
Static method to create and return an new instance of a simple LRU cache.
|
int |
hashCode() |
protected boolean |
removeEldestEntry(java.util.Map.Entry eldest) |
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, valuesclone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, sizepublic static <K,V> LruCache<K,V> getCache(int initialCapacity, int maximumEntries, float loadFactor)
K - the key object typeV - the value object typeinitialCapacity - the initial capacity of the cachemaximumEntries - the maximum number of entries allowed in this cacheloadFactor - the map load factorLruCacheprotected boolean removeEldestEntry(java.util.Map.Entry eldest)
public boolean equals(java.lang.Object object)