Map
implementation with a fixed maximum size which removes the least recently used entry if an entry is added when full. The least recently used algorithm works on the get and put operations only. Iteration of any kind, including setting the value by iteration, does not change the order. Queries such as containsKey and containsValue or access via views also do not change the order.
A somewhat subtle ramification of the least recently used algorithm is that calls to {@link #get(Object)} stand a very good chanceof modifying the map's iteration order and thus invalidating any iterators currently in use. It is therefore suggested that iterations over an {@link LRUMap} instance access entry values only through a{@link org.apache.commons.collections4.MapIterator MapIterator} or {@link #entrySet()} iterator.
The map implements OrderedMap
and entries may be queried using the bidirectional OrderedMapIterator
. The order returned is least recently used to most recently used. Iterators from map views can also be cast to OrderedIterator
if required.
All the available iterators can be reset back to the start by casting to ResettableIterator
and calling reset()
.
Note that LRUMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw NullPointerException
's when accessed by concurrent threads.
@since 3.0 (previously in main package v1.0)
@version $Id: LRUMap.java 1477799 2013-04-30 19:56:11Z tn $
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|