public class LinkedHashMap extends HashMap implements Rollbackable
It uses a hash-bucket approach; that is, hash collisions are handled by linking the new node off of the pre-existing node (or list of nodes). In this manner, techniques such as linear probing (which can cause primary clustering) and rehashing (which does not fit very well with Java's method of precomputing hash codes) are avoided. In addition, this maintains a doubly-linked list which tracks either insertion or access order.
In insertion order, calling put
adds the key to the end of
traversal, unless the key was already in the map; changing traversal order
requires removing and reinserting a key. On the other hand, in access
order, all calls to put
and get
cause the
accessed key to move to the end of the traversal list. Note that any
accesses to the map's contents via its collection views and iterators do
not affect the map's traversal order, since the collection views do not
call put
or get
.
One of the nice features of tracking insertion order is that you can
copy a hashtable, and regardless of the implementation of the original,
produce the same results when iterating over the copy. This is possible
without needing the overhead of TreeMap
.
When using this {
Object.hashCode()
,
Collection
,
Map
,
HashMap
,
TreeMap
,
Hashtable
,
Serialized Form$CHECKPOINT, $RECORD$$CHECKPOINT
Constructor and Description |
---|
LinkedHashMap()
Construct a new insertion-ordered LinkedHashMap with the default
capacity (11) and the default load factor (0.75).
|
LinkedHashMap(int initialCapacity)
Construct a new insertion-ordered LinkedHashMap with a specific
inital capacity and default load factor of 0.75.
|
LinkedHashMap(int initialCapacity,
float loadFactor)
Construct a new insertion-orderd LinkedHashMap with a specific
inital capacity and load factor.
|
LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder)
Construct a new LinkedHashMap with a specific inital capacity, load
factor, and ordering mode.
|
LinkedHashMap(Map m)
Construct a new insertion-ordered LinkedHashMap from the given Map,
with initial capacity the greater of the size of
m or
the default of 11. |
Modifier and Type | Method and Description |
---|---|
void |
$COMMIT(long timestamp)
Commit changes up to the given timestamp, but not including changes
made at timestamp and afterward.
|
void |
$RESTORE(long timestamp,
boolean trim)
Restore a previous state to all the private fields of this rollbackable
object.
|
void |
clear()
Clears the Map so it has no keys.
|
boolean |
containsValue(java.lang.Object value)
Returns
true if this HashMap contains a value
o , such that o.equals(value) . |
java.lang.Object |
get(java.lang.Object key)
Return the value in this Map associated with the supplied key,
or
null if the key maps to nothing. |
protected boolean |
removeEldestEntry(Map.Entry eldest)
Returns
true if this map should remove the eldest entry. |
clone, containsKey, entrySet, isEmpty, keySet, put, putAll, remove, size, values
$GET$CHECKPOINT, $SET$CHECKPOINT, equals, hashCode, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
$GET$CHECKPOINT, $SET$CHECKPOINT
$GET$CHECKPOINT, $SET$CHECKPOINT, equals, hashCode
public LinkedHashMap()
public LinkedHashMap(Map m)
m
or
the default of 11.
Every element in Map m will be put into this new HashMap, in the order of m's iterator.
m
- a Map whose key / value pairs will be put into
the new HashMap. NOTE: key / value pairs
are not cloned in this constructor.java.lang.NullPointerException
- if m is nullpublic LinkedHashMap(int initialCapacity)
initialCapacity
- the initial capacity of this HashMap (>= 0)java.lang.IllegalArgumentException
- if (initialCapacity < 0)public LinkedHashMap(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity (>= 0)loadFactor
- the load factor (> 0, not NaN)java.lang.IllegalArgumentException
- if (initialCapacity < 0) ||
! (loadFactor > 0.0)public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
initialCapacity
- the initial capacity (>=0)loadFactor
- the load factor (>0, not NaN)accessOrder
- true for access-order, false for insertion-orderjava.lang.IllegalArgumentException
- if (initialCapacity < 0) ||
! (loadFactor > 0.0)public void clear()
clear
in interface Map
clear
in class HashMap
Set.clear()
public boolean containsValue(java.lang.Object value)
true
if this HashMap contains a value
o
, such that o.equals(value)
.containsValue
in interface Map
containsValue
in class HashMap
value
- the value to search for in this HashMaptrue
if at least one key maps to the valueHashMap.containsKey(Object)
public java.lang.Object get(java.lang.Object key)
null
if the key maps to nothing. If this is an
access-ordered Map and the key is found, this performs structural
modification, moving the key to the newest end of the list. NOTE:
Since the value could also be null, you must use containsKey to
see if this key actually maps to something.get
in interface Map
get
in class HashMap
key
- the key for which to fetch an associated valueHashMap.put(Object, Object)
,
HashMap.containsKey(Object)
protected boolean removeEldestEntry(Map.Entry eldest)
true
if this map should remove the eldest entry.
This method is invoked by all calls to put
and
putAll
which place a new entry in the map, providing
the implementer an opportunity to remove the eldest entry any time
a new one is added. This can be used to save memory usage of the
hashtable, as well as emulating a cache, by deleting stale entries.
For example, to keep the Map limited to 100 entries, override as follows:
private static final int MAX_ENTRIES = 100; protected boolean removeEldestEntry(Map.Entry eldest) { return size() > MAX_ENTRIES; }
Typically, this method does not modify the map, but just uses the
return value as an indication to put
whether to proceed.
However, if you override it to modify the map, you must return false
(indicating that put
should leave the modified map alone),
or you face unspecified behavior. Remember that in access-order mode,
even calling get
is a structural modification, but using
the collections views (such as keySet
) is not.
This method is called after the eldest entry has been inserted, so
if put
was called on a previously empty map, the eldest
entry is the one you just put in! The default implementation just
returns false
, so that this map always behaves like
a normal one with unbounded growth.
eldest
- the eldest element which would be removed if this
returns true. For an access-order map, this is the least
recently accessed; for an insertion-order map, this is the
earliest element inserted.eldest
should be removedpublic void $COMMIT(long timestamp)
Rollbackable
public void $RESTORE(long timestamp, boolean trim)
Rollbackable
$RESTORE
in interface Rollbackable
$RESTORE
in interface Map
$RESTORE
in class HashMap
timestamp
- The timestamp taken at the time when the previous
state was recorded.trim
- Whether to delete the records used for the rollback.Checkpoint.rollback(long, boolean)