Class LRUMap
- All Implemented Interfaces:
Externalizable,Serializable,Cloneable,Map
An implementation of a Map which has a maximum size and uses a Least Recently Used algorithm to remove items from the Map when the maximum size is reached and new items are added.
A synchronized version can be obtained with:
Collections.synchronizedMap( theMapToSynchronize )
If it will be accessed by multiple threads, you _must_ synchronize access
to this Map. Even concurrent get(Object) operations produce indeterminate
behaviour.
Unlike the Collections 1.0 version, this version of LRUMap does use a true LRU algorithm. The keys for all gets and puts are moved to the front of the list. LRUMap is now a subclass of SequencedHashMap, and the "LRU" key is now equivalent to LRUMap.getFirst().
- Since:
- Commons Collections 1.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDeprecated.Get the value for a key from the Map.intDeprecated.Getter for property maximumSize.Deprecated.Removes the key and its Object from the Map.voidDeprecated.Deserializes this map from the given stream.voidsetMaximumSize(int maximumSize) Deprecated.Setter for property maximumSize.voidDeprecated.Serializes this map to the given stream.Methods inherited from class org.apache.commons.collections.SequencedHashMap
clear, clone, containsKey, containsValue, entrySet, equals, get, getFirst, getFirstKey, getFirstValue, getLast, getLastKey, getLastValue, getValue, hashCode, indexOf, isEmpty, iterator, keySet, lastIndexOf, putAll, remove, remove, sequence, size, toString, valuesMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
LRUMap
public LRUMap()Deprecated.Default constructor, primarily for the purpose of de-externalization. This constructors sets a default LRU limit of 100 keys, but this value may be overridden internally as a result of de-externalization. -
LRUMap
public LRUMap(int i) Deprecated.Create a new LRUMap with a maximum capacity of i. Once i capacity is achieved, subsequent gets and puts will push keys out of the map. See .- Parameters:
i- Maximum capacity of the LRUMap
-
-
Method Details
-
get
Deprecated.Get the value for a key from the Map. The key will be promoted to the Most Recently Used position. Note that get(Object) operations will modify the underlying Collection. Calling get(Object) inside of an iteration over keys, values, etc. is currently unsupported.
- Specified by:
getin interfaceMap- Overrides:
getin classSequencedHashMap- Parameters:
key- Key to retrieve- Returns:
- Returns the value. Returns null if the key has a null value or if the key has no value.
-
put
Deprecated.Removes the key and its Object from the Map.
(Note: this may result in the "Least Recently Used" object being removed from the Map. In that case, the removeLRU() method is called. See javadoc for removeLRU() for more details.)
- Specified by:
putin interfaceMap- Overrides:
putin classSequencedHashMap- Parameters:
key- Key of the Object to add.value- Object to add- Returns:
- Former value of the key
-
readExternal
Deprecated.Description copied from class:SequencedHashMapDeserializes this map from the given stream.- Specified by:
readExternalin interfaceExternalizable- Overrides:
readExternalin classSequencedHashMap- Parameters:
in- the stream to deserialize from- Throws:
IOException- if the stream raises itClassNotFoundException- if the stream raises it
-
writeExternal
Deprecated.Description copied from class:SequencedHashMapSerializes this map to the given stream.- Specified by:
writeExternalin interfaceExternalizable- Overrides:
writeExternalin classSequencedHashMap- Parameters:
out- the stream to serialize to- Throws:
IOException- if the stream raises it
-
getMaximumSize
public int getMaximumSize()Deprecated.Getter for property maximumSize.- Returns:
- Value of property maximumSize.
-
setMaximumSize
public void setMaximumSize(int maximumSize) Deprecated.Setter for property maximumSize.- Parameters:
maximumSize- New value of property maximumSize.
-