Class LinkedMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- com.twelvemonkeys.util.LinkedMap<K,V>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.util.Map<K,V>
- Direct Known Subclasses:
LRUMap
public class LinkedMap<K,V> extends java.util.AbstractMap<K,V> implements java.io.SerializableGeneric map and linked list implementation of theMapinterface, with predictable iteration order.Resembles
LinkedHashMapfrom JDK 1.4+, but is backed by a genericMap, rather than implementing a particular algoritm.This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map (a key
kis reinserted into a mapmifm.put(k, v)is invoked whenm.containsKey(k)would returntrueimmediately prior to the invocation).A special
constructoris provided to create a linked hash map whose order of iteration is the order in which its entries were last accessed, from least-recently accessed to most-recently (access-order). This kind of map is well-suited to building LRU caches. Invoking theputorgetmethod results in an access to the corresponding entry (assuming it exists after the invocation completes). TheputAllmethod generates one entry access for each mapping in the specified map, in the order that key-value mappings are provided by the specified map's entry set iterator. No other methods generate entry accesses. In particular, operations on collection-views do not affect the order of iteration of the backing map.The
removeEldestEntry(Map.Entry)method may be overridden to impose a policy for removing stale mappings automatically when new mappings are added to the map.- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/LinkedMap.java#1 $
- Author:
- inspired by LinkedHashMap from JDK 1.4+, by Josh Bloch, Harald Kuhr
- See Also:
LinkedHashMap,LRUMap, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classLinkedMap.LinkedEntry<K,V>Linked list implementation ofMap.Entry.
-
Field Summary
Fields Modifier and Type Field Description protected booleanaccessOrderprotected java.util.Map<K,java.util.Map.Entry<K,V>>entriesprotected intmodCount
-
Constructor Summary
Constructors Constructor Description LinkedMap()Creates aLinkedMapbacked by aHashMap, with default (insertion) order.LinkedMap(boolean pAccessOrder)Creates aLinkedMapbacked by aHashMap, with the given order.LinkedMap(java.util.Map<? extends K,? extends V> pContents)Creates aLinkedMapbacked by aHashMap, with key/value pairs copied frompContentsand default (insertion) order.LinkedMap(java.util.Map<? extends K,? extends V> pContents, boolean pAccessOrder)Creates aLinkedMapbacked by aHashMap, with key/value pairs copied frompContentsand the given order.LinkedMap(java.util.Map<K,java.util.Map.Entry<K,V>> pBacking, java.util.Map<? extends K,? extends V> pContents)Creates aLinkedMapbacked by the given map, with key/value pairs copied frompContentsand default (insertion) order.LinkedMap(java.util.Map<K,java.util.Map.Entry<K,V>> pBacking, java.util.Map<? extends K,? extends V> pContents, boolean pAccessOrder)Creates aLinkedMapbacked by the given map, with key/value pairs copied frompContentsand the given order.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()java.lang.Objectclone()Returns a shallow copy of thisAbstractMapinstance: the keys and values themselves are not cloned.booleancontainsKey(java.lang.Object pKey)booleancontainsValue(java.lang.Object pValue)Returnstrueif this map maps one or more keys to the specified pValue.java.util.Set<java.util.Map.Entry<K,V>>entrySet()Vget(java.lang.Object pKey)protected voidinit()Default implementation, does nothing.booleanisEmpty()java.util.Set<K>keySet()protected java.util.Iterator<java.util.Map.Entry<K,V>>newEntryIterator()protected java.util.Iterator<K>newKeyIterator()protected java.util.Iterator<V>newValueIterator()Vput(K pKey, V pValue)Vremove(java.lang.Object pKey)protected booleanremoveEldestEntry(java.util.Map.Entry<K,V> pEldest)Returnstrueif this map should remove its eldest entry.protected java.util.Map.Entry<K,V>removeEntry(java.util.Map.Entry<K,V> pEntry)Removes the given entry from the Map.intsize()java.util.Collection<V>values()
-
-
-
Constructor Detail
-
LinkedMap
public LinkedMap()
Creates aLinkedMapbacked by aHashMap, with default (insertion) order.
-
LinkedMap
public LinkedMap(boolean pAccessOrder)
Creates aLinkedMapbacked by aHashMap, with the given order.- Parameters:
pAccessOrder- iftrue, ordering will be "least recently accessed item" to "latest accessed item", otherwise "first inserted item" to "latest inserted item".
-
LinkedMap
public LinkedMap(java.util.Map<? extends K,? extends V> pContents)
Creates aLinkedMapbacked by aHashMap, with key/value pairs copied frompContentsand default (insertion) order.- Parameters:
pContents- the map whose mappings are to be placed in this map. May benull.
-
LinkedMap
public LinkedMap(java.util.Map<? extends K,? extends V> pContents, boolean pAccessOrder)
Creates aLinkedMapbacked by aHashMap, with key/value pairs copied frompContentsand the given order.- Parameters:
pContents- the map whose mappings are to be placed in this map. May benull.pAccessOrder- iftrue, ordering will be "least recently accessed item" to "latest accessed item", otherwise "first inserted item" to "latest inserted item".
-
LinkedMap
public LinkedMap(java.util.Map<K,java.util.Map.Entry<K,V>> pBacking, java.util.Map<? extends K,? extends V> pContents)
Creates aLinkedMapbacked by the given map, with key/value pairs copied frompContentsand default (insertion) order.- Parameters:
pBacking- the backing map of this map. Must be either empty, or the same map aspContents.pContents- the map whose mappings are to be placed in this map. May benull.
-
LinkedMap
public LinkedMap(java.util.Map<K,java.util.Map.Entry<K,V>> pBacking, java.util.Map<? extends K,? extends V> pContents, boolean pAccessOrder)
Creates aLinkedMapbacked by the given map, with key/value pairs copied frompContentsand the given order.- Parameters:
pBacking- the backing map of this map. Must be either empty, or the same map aspContents.pContents- the map whose mappings are to be placed in this map. May benull.pAccessOrder- iftrue, ordering will be "least recently accessed item" to "latest accessed item", otherwise "first inserted item" to "latest inserted item".
-
-
Method Detail
-
init
protected void init()
Default implementation, does nothing.
-
containsValue
public boolean containsValue(java.lang.Object pValue)
Returnstrueif this map maps one or more keys to the specified pValue. More formally, returnstrueif and only if this map contains at least one mapping to a pValuevsuch that(pValue==null ? v==null : pValue.equals(v)).This implementation requires time linear in the map size for this operation.
-
newKeyIterator
protected java.util.Iterator<K> newKeyIterator()
-
newValueIterator
protected java.util.Iterator<V> newValueIterator()
-
remove
public V remove(java.lang.Object pKey)
-
clone
public java.lang.Object clone() throws java.lang.CloneNotSupportedExceptionReturns a shallow copy of thisAbstractMapinstance: the keys and values themselves are not cloned.- Returns:
- a copy of this map, with the same order and same key/value pairs.
- Throws:
java.lang.CloneNotSupportedException
-
removeEldestEntry
protected boolean removeEldestEntry(java.util.Map.Entry<K,V> pEldest)
Returnstrueif this map should remove its eldest entry. This method is invoked byputandputAllafter inserting a new entry into the map. It provides the implementer with the opportunity to remove the eldest entry each time a new one is added. This is useful if the map represents a cache: it allows the map to reduce memory consumption by deleting stale entries.Sample use: this override will allow the map to grow up to 100 entries and then delete the eldest entry each time a new entry is added, maintaining a steady state of 100 entries.
private static final int MAX_ENTRIES = 100; protected boolean removeEldestEntry(Map.Entry eldest) { return size() > MAX_ENTRIES; }This method typically does not modify the map in any way, instead allowing the map to modify itself as directed by its return value. It is permitted for this method to modify the map directly, but if it does so, it must return
false(indicating that the map should not attempt any further modification). The effects of returningtrueafter modifying the map from within this method are unspecified.This implementation merely returns
false(so that this map acts like a normal map - the eldest element is never removed).- Parameters:
pEldest- The least recently inserted entry in the map, or if this is an access-ordered map, the least recently accessed entry. This is the entry that will be removed it this method returnstrue. If the map was empty prior to theputorputAllinvocation resulting in this invocation, this will be the entry that was just inserted; in other words, if the map contains a single entry, the eldest entry is also the newest.- Returns:
trueif the eldest entry should be removed from the map;falseif it should be retained.
-
size
public int size()
- Specified by:
sizein interfacejava.util.Map<K,V>- Overrides:
sizein classjava.util.AbstractMap<K,V>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map<K,V>- Overrides:
clearin classjava.util.AbstractMap<K,V>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Map<K,V>- Overrides:
isEmptyin classjava.util.AbstractMap<K,V>
-
containsKey
public boolean containsKey(java.lang.Object pKey)
- Specified by:
containsKeyin interfacejava.util.Map<K,V>- Overrides:
containsKeyin classjava.util.AbstractMap<K,V>
-
values
public java.util.Collection<V> values()
- Specified by:
valuesin interfacejava.util.Map<K,V>- Overrides:
valuesin classjava.util.AbstractMap<K,V>
-
entrySet
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
- Specified by:
entrySetin interfacejava.util.Map<K,V>- Specified by:
entrySetin classjava.util.AbstractMap<K,V>
-
keySet
public java.util.Set<K> keySet()
- Specified by:
keySetin interfacejava.util.Map<K,V>- Overrides:
keySetin classjava.util.AbstractMap<K,V>
-
removeEntry
protected java.util.Map.Entry<K,V> removeEntry(java.util.Map.Entry<K,V> pEntry)
Removes the given entry from the Map.- Parameters:
pEntry- the entry to be removed- Returns:
- the removed entry, or
nullif nothing was removed.
-
-