org.limewire.collection
Class ForgetfulHashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
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
|
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 |
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.
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>
Copyright © 2008 Lime Wire LLC. All Rights Reserved.