Class LinkedMap<K,V>
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<K,
V>
- Direct Known Subclasses:
LRUMap
Map
interface,
with predictable iteration order.
Resembles LinkedHashMap
from JDK 1.4+, but is backed by a generic
Map
, 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 k
is reinserted into a map m
if
m.put(k, v)
is invoked when m.containsKey(k)
would return
true
immediately prior to the invocation).
A special constructor
is 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 the put
or get
method results in an access to the
corresponding entry (assuming it exists after the invocation completes).
The putAll
method 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 $
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
private class
protected static class
Linked list implementation ofMap.Entry
.private class
private class
Nested classes/interfaces inherited from class com.twelvemonkeys.util.AbstractDecoratedMap
AbstractDecoratedMap.BasicEntry<K,
V>, AbstractDecoratedMap.EntrySet, AbstractDecoratedMap.KeySet, AbstractDecoratedMap.Values Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final boolean
(package private) LinkedMap.LinkedEntry
<K, V> Fields inherited from class com.twelvemonkeys.util.AbstractDecoratedMap
entries, modCount
-
Constructor Summary
ConstructorsConstructorDescriptionCreates aLinkedMap
backed by aHashMap
, with default (insertion) order.LinkedMap
(boolean pAccessOrder) Creates aLinkedMap
backed by aHashMap
, with the given order.Creates aLinkedMap
backed by aHashMap
, with key/value pairs copied frompContents
and default (insertion) order.Creates aLinkedMap
backed by aHashMap
, with key/value pairs copied frompContents
and the given order.Creates aLinkedMap
backed by the given map, with key/value pairs copied frompContents
and default (insertion) order.LinkedMap
(Map<K, Map.Entry<K, V>> pBacking, Map<? extends K, ? extends V> pContents, boolean pAccessOrder) Creates aLinkedMap
backed by the given map, with key/value pairs copied frompContents
and the given order. -
Method Summary
Modifier and TypeMethodDescriptionclone()
Returns a shallow copy of thisAbstractMap
instance: the keys and values themselves are not cloned.boolean
containsValue
(Object pValue) Returnstrue
if this map maps one or more keys to the specified pValue.(package private) LinkedMap.LinkedEntry
<K, V> createEntry
(K pKey, V pValue) Creates a newLinkedEntry
.protected void
init()
Default implementation, does nothing.protected boolean
removeEldestEntry
(Map.Entry<K, V> pEldest) Returnstrue
if this map should remove its eldest entry.Methods inherited from class com.twelvemonkeys.util.AbstractDecoratedMap
clear, containsKey, entrySet, getEntry, isEmpty, keySet, removeEntry, size, values
Methods inherited from class java.util.AbstractMap
equals, hashCode, putAll, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
head
-
accessOrder
protected final boolean accessOrder
-
-
Constructor Details
-
LinkedMap
public LinkedMap()Creates aLinkedMap
backed by aHashMap
, with default (insertion) order. -
LinkedMap
public LinkedMap(boolean pAccessOrder) Creates aLinkedMap
backed 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
Creates aLinkedMap
backed by aHashMap
, with key/value pairs copied frompContents
and default (insertion) order.- Parameters:
pContents
- the map whose mappings are to be placed in this map. May benull
.
-
LinkedMap
Creates aLinkedMap
backed by aHashMap
, with key/value pairs copied frompContents
and 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
Creates aLinkedMap
backed by the given map, with key/value pairs copied frompContents
and 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(Map<K, Map.Entry<K, V>> pBacking, Map<? extends K, ? extends V> pContents, boolean pAccessOrder) Creates aLinkedMap
backed by the given map, with key/value pairs copied frompContents
and 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 Details
-
init
protected void init()Description copied from class:AbstractDecoratedMap
Default implementation, does nothing.- Overrides:
init
in classAbstractDecoratedMap<K,
V>
-
containsValue
Description copied from class:AbstractDecoratedMap
Returnstrue
if this map maps one or more keys to the specified pValue. More formally, returnstrue
if and only if this map contains at least one mapping to a pValuev
such that(pValue==null ? v==null : pValue.equals(v))
.This implementation requires time linear in the map size for this operation.
- Specified by:
containsValue
in interfaceMap<K,
V> - Overrides:
containsValue
in classAbstractDecoratedMap<K,
V> - Parameters:
pValue
- pValue whose presence in this map is to be tested.- Returns:
true
if this map maps one or more keys to the specified pValue.
-
newKeyIterator
- Specified by:
newKeyIterator
in classAbstractDecoratedMap<K,
V>
-
newValueIterator
- Specified by:
newValueIterator
in classAbstractDecoratedMap<K,
V>
-
newEntryIterator
- Specified by:
newEntryIterator
in classAbstractDecoratedMap<K,
V>
-
get
-
remove
-
put
-
createEntry
Creates a newLinkedEntry
.- Overrides:
createEntry
in classAbstractDecoratedMap<K,
V> - Parameters:
pKey
- the keypValue
- the value- Returns:
- a new LinkedEntry
-
clone
Description copied from class:AbstractDecoratedMap
Returns a shallow copy of thisAbstractMap
instance: the keys and values themselves are not cloned.- Overrides:
clone
in classAbstractDecoratedMap<K,
V> - Returns:
- a copy of this map, with the same order and same key/value pairs.
- Throws:
CloneNotSupportedException
-
removeEldestEntry
Returnstrue
if this map should remove its eldest entry. This method is invoked byput
andputAll
after 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 returningtrue
after 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 theput
orputAll
invocation 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:
true
if the eldest entry should be removed from the map;false
if it should be retained.
-