org.apache.commons.collections
Class LRUMap

java.lang.Object
  |
  +--java.util.AbstractMap
        |
        +--java.util.HashMap
              |
              +--org.apache.commons.collections.LRUMap
All Implemented Interfaces:
Cloneable, Externalizable, Map, Serializable

public class LRUMap
extends HashMap
implements Externalizable

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.

This implementation uses a simple bubbling algorithm, whereby every random access get() method call bubbles the item up the list, further away from the 'drop zone'.

A synchronized version can be obtained with: Collections.synchronizedMap( theMapToSynchronize )

WARNING the values() and entrySet() methods require optimisation like the standard HashMap implementations so that iteration over this Map is efficient.

Author:
James Strachan
See Also:
Serialized Form

Inner classes inherited from class java.util.Map
Map.Entry
 
Constructor Summary
LRUMap()
           
LRUMap(int i)
           
 
Method Summary
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set entrySet()
           
 Object get(Object key)
           
 int getMaximumSize()
          Getter for property maximumSize.
 Set keySet()
           
 Object put(Object key, Object value)
           
 void readExternal(ObjectInput in)
           
 Object remove(Object key)
           
 Object removeLRU()
          Removes the least recently used object from the Map.
 void setMaximumSize(int maximumSize)
          Setter for property maximumSize.
 Collection values()
           
 void writeExternal(ObjectOutput out)
           
 
Methods inherited from class java.util.HashMap
clear, clone, isEmpty, putAll, size
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

LRUMap

public LRUMap()

LRUMap

public LRUMap(int i)
Method Detail

containsKey

public boolean containsKey(Object key)
Overrides:
containsKey in class HashMap

containsValue

public boolean containsValue(Object value)
Overrides:
containsValue in class HashMap

entrySet

public Set entrySet()
Overrides:
entrySet in class HashMap

get

public Object get(Object key)
Overrides:
get in class HashMap

getMaximumSize

public int getMaximumSize()
Getter for property maximumSize.
Returns:
Value of property maximumSize.

keySet

public Set keySet()
Overrides:
keySet in class HashMap

put

public Object put(Object key,
                  Object value)
Overrides:
put in class HashMap

readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Specified by:
readExternal in interface Externalizable

remove

public Object remove(Object key)
Overrides:
remove in class HashMap

removeLRU

public Object removeLRU()
Removes the least recently used object from the Map.
Returns:
the key of the removed item

setMaximumSize

public void setMaximumSize(int maximumSize)
Setter for property maximumSize.
Parameters:
maximumSize - New value of property maximumSize.

values

public Collection values()
Overrides:
values in class HashMap

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Specified by:
writeExternal in interface Externalizable


Copyright © 2001 Apache Software Foundation. Documenation generated July 14 2001.