Class LRUHashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
java.util.LinkedHashMap<K,V>
com.twelvemonkeys.util.LRUHashMap<K,V>
- All Implemented Interfaces:
ExpiringMap<K,V>, Serializable, Cloneable, Map<K, V>, SequencedMap<K, V>
Map implementation with size limit, that keeps its entries in LRU
(least recently used) order, also known as access-order.
When the size limit is reached, the least recently accessed mappings are
removed. The number of mappings to be removed from the map, is
controlled by the trim factor.
- Default size limit is 1000 elements.
See
setMaxSize(int)/getMaxSize(). - Default trim factor is 1% (
0.01f). SeesetTrimFactor(float)/getTrimFactor().
- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/LRUHashMap.java#1 $
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates an LRUHashMap with default max size (1000 entries).LRUHashMap(int pMaxSize) Creates an LRUHashMap with the given max size.LRUHashMap(Map<? extends K, ? extends V> pContents) Creates an LRUHashMap with initial mappings from the given map, and default max size (1000 entries).LRUHashMap(Map<? extends K, ? extends V> pContents, int pMaxSize) Creates an LRUHashMap with initial mappings from the given map, and the given max size. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the maximum number of mappings in this map.floatReturns the current trim factor.voidprocessRemoved(Map.Entry<K, V> pRemoved) Default implementation does nothing.protected booleanremoveEldestEntry(Map.Entry<K, V> pEldest) always returnsfalse, and instead invokesremoveLRU()ifsize >= maxSize.voidRemoves the least recently used mapping(s) from this map.voidsetMaxSize(int pMaxSize) Sets the maximum number of elements in this map.voidsetTrimFactor(float pTrimFactor) Sets the trim factor.Methods inherited from class LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, newLinkedHashMap, putFirst, putLast, replaceAll, reversed, sequencedEntrySet, sequencedKeySet, sequencedValues, valuesMethods inherited from class HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, newHashMap, put, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from class AbstractMap
equals, hashCode, toStringMethods inherited from interface Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesMethods inherited from interface SequencedMap
firstEntry, lastEntry, pollFirstEntry, pollLastEntry
-
Field Details
-
maxSize
private int maxSize -
trimFactor
private float trimFactor
-
-
Constructor Details
-
LRUHashMap
public LRUHashMap()Creates an LRUHashMap with default max size (1000 entries). This is constructor is here to comply with the reccomendations for "standard" constructors in theMapinterface.- See Also:
-
LRUHashMap
public LRUHashMap(int pMaxSize) Creates an LRUHashMap with the given max size.- Parameters:
pMaxSize- size limit
-
LRUHashMap
Creates an LRUHashMap with initial mappings from the given map, and default max size (1000 entries). This is constructor is here to comply with the reccomendations for "standard" constructors in theMapinterface.- Parameters:
pContents- the map whose mappings are to be placed in this map. May benull.- See Also:
-
LRUHashMap
-
-
Method Details
-
getMaxSize
public int getMaxSize()Returns the maximum number of mappings in this map.- Returns:
- the size limit
-
setMaxSize
public void setMaxSize(int pMaxSize) Sets the maximum number of elements in this map. If the current size is greater than the new max size, the map will be trimmed to fit the new max size constraint.- Parameters:
pMaxSize- new size limit- See Also:
-
getTrimFactor
public float getTrimFactor()Returns the current trim factor.The trim factor controls how many percent of the maps current size is reclaimed, when performing an
removeLRUoperation. Defaults to 1% (0.01f).- Returns:
- the current trim factor
-
setTrimFactor
public void setTrimFactor(float pTrimFactor) Sets the trim factor.The trim factor controls how many percent of the maps current size is reclaimed, when performing an
removeLRUoperation. Defaults to 1% (0.01f).- Parameters:
pTrimFactor- the new trim factor. Acceptable values are between 0 (inclusive) and 1 (exclusive).- See Also:
-
removeEldestEntry
always returnsfalse, and instead invokesremoveLRU()ifsize >= maxSize.- Overrides:
removeEldestEntryin classLinkedHashMap<K,V>
-
processRemoved
Default implementation does nothing. May be used by clients as a call-back to notify when mappings expire from the map.- Specified by:
processRemovedin interfaceExpiringMap<K,V> - Parameters:
pRemoved- the removed mapping
-
removeLRU
public void removeLRU()Removes the least recently used mapping(s) from this map.How many mappings are removed from the map, is controlled by the trim factor. In any case, at least one mapping will be removed.
- See Also:
-