LimeWire Collection Component API

org.limewire.collection
Class ForgetfulHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by org.limewire.collection.ForgetfulHashMap<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>

public class ForgetfulHashMap<K,V>
extends HashMap<K,V>

A mapping that "forgets" keys and values using a FIFO replacement policy, much like a cache.

More formally, a ForgetfulHashMap is a sequence of key-value pairs [ (K1, V1), ... (KN, VN) ] ordered from youngest to oldest. When inserting a new pair, (KN, VN) is discarded if N is greater than a size (this threshold is fixed when the table is constructed). However, this property may not hold if keys are re-mapped to different values; if you need to re-map keys to different values, use FixedsizeForgetfulHashMap.

Technically, this is not a valid subtype of HashMap (since items can be removed), but HashMap makes implementation easy.

This class is not thread-safe.

    ForgetfulHashMap<String, String> fhm = new ForgetfulHashMap<String, String>(2);
    fhm.put("myKey1", "Abby");
    fhm.put("myKey2", "Bob");
    System.out.println(fhm);
    fhm.put("myKey3", "Chris");
    System.out.println(fhm);
    fhm.put("myKey3", "replace");
    System.out.println(fhm);

    Output:  
        {myKey2=Bob, myKey1=Abby}
        {myKey2=Bob, myKey3=Chris}
        {myKey3=replace}

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
ForgetfulHashMap(int size)
          Create a new ForgetfulHashMap that holds only the last "size" entries.
 
Method Summary
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> t)
          Calls put on all keys in t.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

ForgetfulHashMap

public ForgetfulHashMap(int size)
Create a new ForgetfulHashMap that holds only the last "size" entries.

Parameters:
size - the number of entries to hold
Throws:
IllegalArgumentException - if size is less < 1.
Method Detail

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>
Overrides:
put in class HashMap<K,V>

putAll

public void putAll(Map<? extends K,? extends V> t)
Calls put on all keys in t. See put(Object, Object) for specification.

Specified by:
putAll in interface Map<K,V>
Overrides:
putAll in class HashMap<K,V>

LimeWire Collection Component API

Copyright © 2008 Lime Wire LLC. All Rights Reserved.