java.lang.Object
java.util.AbstractMap
java.util.HashMap
java.util.LinkedHashMap
raining.server.LRUHashMap
- All Implemented Interfaces:
- java.lang.Cloneable, java.util.Map, java.io.Serializable
- class LRUHashMap
- extends java.util.LinkedHashMap
An extension of LinkedHashMap that removes eldest accessed entry
after crossing the max_entries.
Pls note that each access of an LRU obviously changes the underlying list.
I need to do some performance testing on this, against some other
implementation, or a home grown one.
| Nested classes inherited from class java.util.HashMap |
|
| Nested classes inherited from class java.util.AbstractMap |
|
| Nested classes inherited from class java.util.Map |
java.util.Map.Entry |
| Fields inherited from class java.util.HashMap |
|
|
Constructor Summary |
LRUHashMap(int initial,
int max_entries,
float loadfactor,
boolean lru)
|
|
Method Summary |
protected boolean |
removeEldestEntry(java.util.Map.Entry eldest)
Returns true if this map should remove the eldest entry. |
max_entries
private int max_entries
LRUHashMap
public LRUHashMap(int initial,
int max_entries,
float loadfactor,
boolean lru)
removeEldestEntry
protected boolean removeEldestEntry(java.util.Map.Entry eldest)
- Description copied from class:
java.util.LinkedHashMap
- Returns
true if this map should remove the eldest entry.
This method is invoked by all calls to put and
putAll which place a new entry in the map, providing
the implementer an opportunity to remove the eldest entry any time
a new one is added. This can be used to save memory usage of the
hashtable, as well as emulating a cache, by deleting stale entries.
For example, to keep the Map limited to 100 entries, override as follows:
private static final int MAX_ENTRIES = 100;
protected boolean removeEldestEntry(Map.Entry eldest)
{
return size() > MAX_ENTRIES;
}
Typically, this method does not modify the map, but just uses the
return value as an indication to put whether to proceed.
However, if you override it to modify the map, you must return false
(indicating that put should leave the modified map alone),
or you face unspecified behavior. Remember that in access-order mode,
even calling get is a structural modification, but using
the collections views (such as keySet) is not.
This method is called after the eldest entry has been inserted, so
if put was called on a previously empty map, the eldest
entry is the one you just put in! The default implementation just
returns false, so that this map always behaves like
a normal one with unbounded growth.