Class LRUMap

All Implemented Interfaces:
Serializable, Cloneable, Map, BoundedMap, IterableMap, OrderedMap

public class LRUMap extends AbstractLinkedMap implements BoundedMap, Serializable, Cloneable
A 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.

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 Collections.synchronizedMap(Map). This class may throw NullPointerException's when accessed by concurrent threads.

Since:
Commons Collections 3.0 (previously in main package v1.0)
Version:
$Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serialisation version
      See Also:
    • DEFAULT_MAX_SIZE

      protected static final int DEFAULT_MAX_SIZE
      Default maximum size
      See Also:
    • maxSize

      private transient int maxSize
      Maximum size
    • scanUntilRemovable

      private boolean scanUntilRemovable
      Scan behaviour
  • Constructor Details

    • LRUMap

      public LRUMap()
      Constructs a new empty map with a maximum size of 100.
    • LRUMap

      public LRUMap(int maxSize)
      Constructs a new, empty map with the specified maximum size.
      Parameters:
      maxSize - the maximum size of the map
      Throws:
      IllegalArgumentException - if the maximum size is less than one
    • LRUMap

      public LRUMap(int maxSize, boolean scanUntilRemovable)
      Constructs a new, empty map with the specified maximum size.
      Parameters:
      maxSize - the maximum size of the map
      scanUntilRemovable - scan until a removeable entry is found, default false
      Throws:
      IllegalArgumentException - if the maximum size is less than one
      Since:
      Commons Collections 3.1
    • LRUMap

      public LRUMap(int maxSize, float loadFactor)
      Constructs a new, empty map with the specified initial capacity and load factor.
      Parameters:
      maxSize - the maximum size of the map, -1 for no limit,
      loadFactor - the load factor
      Throws:
      IllegalArgumentException - if the maximum size is less than one
      IllegalArgumentException - if the load factor is less than zero
    • LRUMap

      public LRUMap(int maxSize, float loadFactor, boolean scanUntilRemovable)
      Constructs a new, empty map with the specified initial capacity and load factor.
      Parameters:
      maxSize - the maximum size of the map, -1 for no limit,
      loadFactor - the load factor
      scanUntilRemovable - scan until a removeable entry is found, default false
      Throws:
      IllegalArgumentException - if the maximum size is less than one
      IllegalArgumentException - if the load factor is less than zero
      Since:
      Commons Collections 3.1
    • LRUMap

      public LRUMap(Map map)
      Constructor copying elements from another map.

      The maximum size is set from the map's size.

      Parameters:
      map - the map to copy
      Throws:
      NullPointerException - if the map is null
      IllegalArgumentException - if the map is empty
    • LRUMap

      public LRUMap(Map map, boolean scanUntilRemovable)
      Constructor copying elements from another map.

      The maximum size is set from the map's size.

      Parameters:
      map - the map to copy
      scanUntilRemovable - scan until a removeable entry is found, default false
      Throws:
      NullPointerException - if the map is null
      IllegalArgumentException - if the map is empty
      Since:
      Commons Collections 3.1
  • Method Details