K
- Type of keysV
- Type of valuespublic class KeyComparatorLinkedHashMap<K,V> extends KeyComparatorHashMap<K,V> implements java.util.Map<K,V>
LinkedHashMap
but supports the
comparison of keys using a KeyComparator
.Modifier and Type | Class and Description |
---|---|
private static class |
KeyComparatorLinkedHashMap.Entry<K,V>
LinkedHashMap entry.
|
private class |
KeyComparatorLinkedHashMap.EntryIterator |
private class |
KeyComparatorLinkedHashMap.KeyIterator |
private class |
KeyComparatorLinkedHashMap.LinkedHashIterator<T> |
private class |
KeyComparatorLinkedHashMap.ValueIterator |
Modifier and Type | Field and Description |
---|---|
private boolean |
accessOrder
The iteration ordering method for this linked hash map: true
for access-order, false for insertion-order.
|
private KeyComparatorLinkedHashMap.Entry<K,V> |
header
The head of the doubly linked list.
|
private static long |
serialVersionUID |
DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, keyComparator, loadFactor, MAXIMUM_CAPACITY, modCount, NULL_KEY, size, table, threshold
Constructor and Description |
---|
KeyComparatorLinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder,
KeyComparator<K> keyComparator)
Constructs an empty LinkedHashMap instance with the
specified initial capacity, load factor and ordering mode.
|
KeyComparatorLinkedHashMap(int initialCapacity,
float loadFactor,
KeyComparator<K> keyComparator)
Constructs an empty insertion-ordered LinkedHashMap instance
with the specified initial capacity and load factor.
|
KeyComparatorLinkedHashMap(int initialCapacity,
KeyComparator<K> keyComparator)
Constructs an empty insertion-ordered LinkedHashMap instance
with the specified initial capacity and a default load factor (0.75).
|
KeyComparatorLinkedHashMap(KeyComparator<K> keyComparator)
Constructs an empty insertion-ordered LinkedHashMap instance
with the default initial capacity (16) and load factor (0.75).
|
KeyComparatorLinkedHashMap(java.util.Map<? extends K,? extends V> m,
KeyComparator<K> keyComparator)
Constructs an insertion-ordered LinkedHashMap instance with
the same mappings as the specified map.
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
addEntry(int hash,
K key,
V value,
int bucketIndex)
This override alters behavior of superclass put method.
|
void |
clear()
Removes all of the mappings from this map.
|
boolean |
containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the
specified value.
|
(package private) void |
createEntry(int hash,
K key,
V value,
int bucketIndex)
This override differs from addEntry in that it doesn't resize the
table or remove the eldest entry.
|
V |
get(java.lang.Object key)
Returns the value to which the specified key is mapped,
or
null if this map contains no mapping for the key. |
(package private) void |
init()
Called by superclass constructors and pseudoconstructors (clone,
readObject) before any entries are inserted into the map.
|
(package private) java.util.Iterator<java.util.Map.Entry<K,V>> |
newEntryIterator() |
(package private) java.util.Iterator<K> |
newKeyIterator() |
(package private) java.util.Iterator<V> |
newValueIterator() |
protected boolean |
removeEldestEntry(java.util.Map.Entry<K,V> eldest)
Returns true if this map should remove its eldest entry.
|
(package private) void |
transfer(KeyComparatorHashMap.Entry[] newTable)
Transfers all entries to new table array.
|
capacity, clone, containsKey, entrySet, eq, getEntry, getModCount, hash, hash, indexFor, isEmpty, isNull, keyComparatorEq, keyComparatorHash, loadFactor, maskNull, put, putAll, remove, removeEntryForKey, removeMapping, resize, size, unmaskNull
private static final long serialVersionUID
private transient KeyComparatorLinkedHashMap.Entry<K,V> header
private final boolean accessOrder
public KeyComparatorLinkedHashMap(int initialCapacity, float loadFactor, KeyComparator<K> keyComparator)
initialCapacity
- the initial capacityloadFactor
- the load factorkeyComparator
- the comparatorjava.lang.IllegalArgumentException
- if the initial capacity is negative
or the load factor is nonpositivepublic KeyComparatorLinkedHashMap(int initialCapacity, KeyComparator<K> keyComparator)
initialCapacity
- the initial capacitykeyComparator
- the comparatorjava.lang.IllegalArgumentException
- if the initial capacity is negativepublic KeyComparatorLinkedHashMap(KeyComparator<K> keyComparator)
keyComparator
- the comparatorpublic KeyComparatorLinkedHashMap(java.util.Map<? extends K,? extends V> m, KeyComparator<K> keyComparator)
m
- the map whose mappings are to be placed in this mapkeyComparator
- the comparatorjava.lang.NullPointerException
- if the specified map is nullpublic KeyComparatorLinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder, KeyComparator<K> keyComparator)
initialCapacity
- the initial capacityloadFactor
- the load factoraccessOrder
- the ordering mode - true for
access-order, false for insertion-orderkeyComparator
- the comparatorjava.lang.IllegalArgumentException
- if the initial capacity is negative
or the load factor is nonpositivevoid init()
init
in class KeyComparatorHashMap<K,V>
void transfer(KeyComparatorHashMap.Entry[] newTable)
transfer
in class KeyComparatorHashMap<K,V>
public boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map<K,V>
containsValue
in class KeyComparatorHashMap<K,V>
value
- value whose presence in this map is to be testedpublic V get(java.lang.Object key)
null
if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key
k
to a value v
such that (key==null ? k==null :
key.equals(k))
, then this method returns v
; otherwise
it returns null
. (There can be at most one such mapping.)
A return value of null
does not necessarily
indicate that the map contains no mapping for the key; it's also
possible that the map explicitly maps the key to null
.
The containsKey
operation may be used to
distinguish these two cases.
get
in interface java.util.Map<K,V>
get
in class KeyComparatorHashMap<K,V>
key
- the key whose associated value is to be returned.KeyComparatorHashMap.put(Object, Object)
public void clear()
java.util.Iterator<K> newKeyIterator()
newKeyIterator
in class KeyComparatorHashMap<K,V>
java.util.Iterator<V> newValueIterator()
newValueIterator
in class KeyComparatorHashMap<K,V>
java.util.Iterator<java.util.Map.Entry<K,V>> newEntryIterator()
newEntryIterator
in class KeyComparatorHashMap<K,V>
void addEntry(int hash, K key, V value, int bucketIndex)
addEntry
in class KeyComparatorHashMap<K,V>
void createEntry(int hash, K key, V value, int bucketIndex)
createEntry
in class KeyComparatorHashMap<K,V>
protected boolean removeEldestEntry(java.util.Map.Entry<K,V> eldest)
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 returning true 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).
eldest
- 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 returns true. If the map was empty prior
to the put or putAll 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.