Most Recently Used Map : Customized Map « Collections Data Structure « Java






Most Recently Used Map

    
/*
 * $Id: MostRecentlyUsedMap.java 457812 2005-10-06 21:20:34Z jdonnerstag $
 * $Revision: 457812 $ $Date: 2005-10-06 23:20:34 +0200 (Thu, 06 Oct 2005) $
 * 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Holds a map of most recently used items of a given maximum size. Old entries
 * are expired when the map exceeds that maximum size.
 * 
 * @author Jonathan Locke
 */
public class MostRecentlyUsedMap extends LinkedHashMap
{
  private static final long serialVersionUID = 1L;

  /** Value most recently removed from map */
  Object removedValue;
  
  /** Maximum number of entries allowed in this map */
  private int maxEntries;

  /**
   * Constructor
   *  
   * @param maxEntries
   *            Maximum number of entries allowed in the map
   */
  public MostRecentlyUsedMap(final int maxEntries)
  {
    super(10, 0.75f, true);

    if (maxEntries <= 0)
    {
      throw new IllegalArgumentException("Must have at least one entry");
    }
    
    this.maxEntries = maxEntries;
  }
  
  /**
   * @return Returns the removedValue.
   */
  public Object getRemovedValue()
  {
    return removedValue;
  }
  
  /**
   * @see java.util.LinkedHashMap#removeEldestEntry(java.util.Map.Entry)
   */
  protected boolean removeEldestEntry(final Map.Entry eldest)
  {
    final boolean remove = size() > maxEntries;
    // when it should be removed remember the oldest value that will be removed
    if (remove)
    {
      this.removedValue = eldest.getValue();
    }
    else
    {
      removedValue = null;
    }
    return remove;
  }
}

   
    
    
    
  








Related examples in the same category

1.Ordered Map
2.Case Insensitive Map
3.A Map collection with real-time behavior
4.Cache Map
5.Map implementation Optimized for Strings keys
6.An integer hashmap
7.An IdentityMap that uses reference-equality instead of object-equality
8.Int Object HashMap
9.Concurrent Skip List Map
10.A hash map that uses primitive ints for the key rather than objects.
11.Integer Map
12.Copy On Write Map
13.Expiring Map
14.Array Map
15.Int Object HashMap (from CERN)
16.Int HashMap from jodd.org
17.String Map
18.List Map
19.Map using Locale objects as keys
20.Map with keys iterated in insertion order
21.Multi Map
22.MultiMap is a Java version of the C++ STL class std::multimap
23.Object Int Map
24.Sequenced HashMap
25.Int Int Map
26.Int Object Map
27.Identity HashMap
28.A java.util.Map interface which can only hold a single object
29.A multi valued Map
30.A simple hashmap from keys to integers
31.A memory-efficient hash map.
32.An implementation of the java.util.Map interface which can only hold a single object.
33.Utility methods for operating on memory-efficient maps.
34.CaseBlindHashMap - a HashMap extension, using Strings as key values.
35.A fixed size map implementation.
36.Int HashMap
37.IntMap provides a simple hashmap from keys to integers
38.Complex Key HashMap
39.A Map with multiple values for a key
40.A Map that accepts int or Integer keys only
41.A Map where keys are compared by object identity, rather than equals()
42.Type-safe Map, from char array to String value
43.A hashtable-based Map implementation with soft keys
44.List ordered map
45.Hash map using String values as keys mapped to primitive int values.
46.Lookup table that stores a list of strings
47.HashNMap stores multiple values by a single key value. Values can be retrieved using a direct query or by creating an enumeration over the stored elements.
48.Combines multiple values to form a single composite key. MultiKey can often be used as an alternative to nested maps.