Package org.simpleframework.common
Class LeastRecentlyUsedMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
java.util.LinkedHashMap<K,V>
org.simpleframework.common.LeastRecentlyUsedMap<K,V>
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<K,
,V> SequencedMap<K,
V>
The
LeastRecentlyUsedMap
is a hash map that keeps only
those entries most recently used. This acts much like a hot spot
cache for specific keys that are used frequently. It also allows
for algorithms to keep hot spot values available in the cache
without the risk of running out of memory.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
TheRemovalListener
is used with the least recently used hash map to listen for removals.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
This is the number of items to keep within the cache.private final LeastRecentlyUsedMap.RemovalListener
<K, V> This is the listener that is called when an entry is removed. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for theLeastRecentlyUsedMap
object.LeastRecentlyUsedMap
(int capacity) Constructor for theLeastRecentlyUsedMap
object.Constructor for theLeastRecentlyUsedMap
object.LeastRecentlyUsedMap
(LeastRecentlyUsedMap.RemovalListener<K, V> listener, int capacity) Constructor for theLeastRecentlyUsedMap
object. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
removeEldestEntry
(Map.Entry<K, V> eldest) This is used to determine if an entry should be removed from the cache.Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, newLinkedHashMap, putFirst, putLast, replaceAll, reversed, sequencedEntrySet, sequencedKeySet, sequencedValues, values
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, newHashMap, put, putAll, putIfAbsent, remove, remove, replace, replace, size
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
Methods inherited from interface java.util.SequencedMap
firstEntry, lastEntry, pollFirstEntry, pollLastEntry
-
Field Details
-
listener
This is the listener that is called when an entry is removed. -
capacity
private final int capacityThis is the number of items to keep within the cache.
-
-
Constructor Details
-
LeastRecentlyUsedMap
public LeastRecentlyUsedMap()Constructor for theLeastRecentlyUsedMap
object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection. -
LeastRecentlyUsedMap
public LeastRecentlyUsedMap(int capacity) Constructor for theLeastRecentlyUsedMap
object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.- Parameters:
capacity
- this is the capacity of the hash container
-
LeastRecentlyUsedMap
Constructor for theLeastRecentlyUsedMap
object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.- Parameters:
listener
- this listens for entries that are removed
-
LeastRecentlyUsedMap
Constructor for theLeastRecentlyUsedMap
object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.- Parameters:
listener
- this listens for entries that are removedcapacity
- this is the capacity of the hash container
-
-
Method Details
-
removeEldestEntry
This is used to determine if an entry should be removed from the cache. If the cache has reached its capacity then the listener, if one was specified is given a callback to tell any other participating objects the entry has been removed.- Overrides:
removeEldestEntry
in classLinkedHashMap<K,
V> - Parameters:
eldest
- this is the candidate for removal
-