K - the type of keys maintained by this mapV - the type of mapped valuespublic abstract class AbstractHashMap<K,V> extends AbstractMap<K,V> implements IterableMap<K,V>
This class implements all the features necessary for a subclass hash-based map.
Key-value entries are stored in instances of the HashEntry class,
which can be overridden and replaced. The iterators can similarly be replaced,
without the need to replace the KeySet, EntrySet and Values view classes.
Overridable methods are provided to change the default hashing behaviour, and to change how entries are added to and removed from the map. Hopefully, all you need for unusual subclasses is here.
NOTE: From Commons Collections 3.1 this class extends AbstractMap. This is to provide backwards compatibility for ReferenceMap between v3.0 and v3.1. This extends clause will be removed in v4.0.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
AbstractHashMap.EntrySet<K,V>
EntrySet implementation.
|
protected static class |
AbstractHashMap.EntrySetIterator<K,V>
EntrySet iterator.
|
protected static class |
AbstractHashMap.HashEntry<K,V>
HashEntry used to store the data.
|
protected static class |
AbstractHashMap.HashIterator<K,V,T>
Base Iterator.
|
protected static class |
AbstractHashMap.HashMapIterator<K,V>
MapIterator implementation.
|
protected static class |
AbstractHashMap.KeySet<K,V>
KeySet implementation.
|
protected static class |
AbstractHashMap.KeySetIterator<K,V>
KeySet iterator.
|
protected static class |
AbstractHashMap.Values<K,V>
Values implementation.
|
protected static class |
AbstractHashMap.ValuesIterator<K,V>
Values iterator.
|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| Modifier and Type | Field and Description |
|---|---|
protected AbstractHashMap.HashEntry<K,V>[] |
data
Map entries
|
protected static int |
DEFAULT_CAPACITY
The default capacity to use
|
protected static float |
DEFAULT_LOAD_FACTOR
The default load factor to use
|
protected static int |
DEFAULT_THRESHOLD
The default threshold to use
|
protected AbstractHashMap.EntrySet |
entrySet
Entry set
|
protected static String |
GETKEY_INVALID |
protected static String |
GETVALUE_INVALID |
protected AbstractHashMap.KeySet |
keySet
Key set
|
protected float |
loadFactor
Load factor, normally 0.75
|
protected static int |
MAXIMUM_CAPACITY
The maximum capacity allowed
|
protected int |
modCount
Modification count for iterators
|
protected static String |
NO_NEXT_ENTRY
Exception messages. |
protected static String |
NO_PREVIOUS_ENTRY |
protected static Object |
NULL
An object for masking null
|
protected static String |
REMOVE_INVALID |
protected static String |
SETVALUE_INVALID |
protected int |
size
The size of the map
|
protected int |
threshold
Size at which to rehash
|
protected AbstractHashMap.Values |
values
Values
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractHashMap()
Constructor only used in deserialization, do not use otherwise.
|
protected |
AbstractHashMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity and
default load factor.
|
protected |
AbstractHashMap(int initialCapacity,
float loadFactor)
Constructs a new, empty map with the specified initial capacity and
load factor.
|
protected |
AbstractHashMap(int initialCapacity,
float loadFactor,
int threshold)
Constructor which performs no validation on the passed in parameters.
|
protected |
AbstractHashMap(Map map)
Constructor copying elements from another map.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
addEntry(AbstractHashMap.HashEntry entry,
int hashIndex)
Adds an entry into this map.
|
protected void |
addMapping(int hashIndex,
int hashCode,
Object key,
Object value)
Adds a new key-value mapping into this map.
|
protected int |
calculateNewCapacity(int proposedCapacity)
Calculates the new capacity of the map.
|
protected int |
calculateThreshold(int newCapacity,
float factor)
Calculates the new threshold of the map, where it will be resized.
|
protected void |
checkCapacity()
Checks the capacity of the map and enlarges it if necessary.
|
void |
clear()
Clears the map, resetting the size to zero and nullifying references
to avoid garbage collection issues.
|
protected Object |
clone()
Clones the map without cloning the keys or values.
|
boolean |
containsKey(Object key)
Checks whether the map contains the specified key.
|
boolean |
containsValue(Object value)
Checks whether the map contains the specified value.
|
protected Object |
convertKey(Object key)
Converts input keys to another object for storage in the map.
|
protected AbstractHashMap.HashEntry |
createEntry(AbstractHashMap.HashEntry next,
int hashCode,
Object key,
Object value)
Creates an entry to store the key-value data.
|
protected Iterator |
createEntrySetIterator()
Creates an entry set iterator.
|
protected Iterator<K> |
createKeySetIterator()
Creates a key set iterator.
|
protected Iterator |
createValuesIterator()
Creates a values iterator.
|
protected void |
destroyEntry(AbstractHashMap.HashEntry entry)
Kills an entry ready for the garbage collector.
|
protected void |
doReadObject(ObjectInputStream in)
Reads the map data from the stream.
|
protected void |
doWriteObject(ObjectOutputStream out)
Writes the map data to the stream.
|
protected void |
ensureCapacity(int newCapacity)
Changes the size of the data structure to the capacity proposed.
|
protected int |
entryHashCode(AbstractHashMap.HashEntry entry)
Gets the
hashCode field from a HashEntry. |
protected Object |
entryKey(AbstractHashMap.HashEntry entry)
Gets the
key field from a HashEntry. |
protected AbstractHashMap.HashEntry |
entryNext(AbstractHashMap.HashEntry entry)
Gets the
next field from a HashEntry. |
Set |
entrySet()
Gets the entrySet view of the map.
|
protected Object |
entryValue(AbstractHashMap.HashEntry entry)
Gets the
value field from a HashEntry. |
boolean |
equals(Object obj)
Compares this map with another.
|
V |
get(Object key)
Gets the value mapped to the key specified.
|
protected AbstractHashMap.HashEntry |
getEntry(Object key)
Gets the entry mapped to the key specified.
|
protected int |
hash(Object key)
Gets the hash code for the key specified.
|
int |
hashCode()
Gets the standard Map hashCode.
|
protected int |
hashIndex(int hashCode,
int dataSize)
Gets the index into the data storage for the hashCode specified.
|
protected void |
init()
Initialise subclasses during construction, cloning or deserialization.
|
boolean |
isEmpty()
Checks whether the map is currently empty.
|
protected boolean |
isEqualKey(Object key1,
Object key2)
Compares two keys, in internal converted form, to see if they are equal.
|
protected boolean |
isEqualValue(Object value1,
Object value2)
Compares two values, in external form, to see if they are equal.
|
Set |
keySet()
Gets the keySet view of the map.
|
MapIterator |
mapIterator()
Gets an iterator over the map.
|
Object |
put(Object key,
Object value)
Puts a key-value mapping into this map.
|
void |
putAll(Map map)
Puts all the values from the specified map into this map.
|
V |
remove(Object key)
Removes the specified mapping from this map.
|
protected void |
removeEntry(AbstractHashMap.HashEntry entry,
int hashIndex,
AbstractHashMap.HashEntry previous)
Removes an entry from the chain stored in a particular index.
|
protected void |
removeMapping(AbstractHashMap.HashEntry entry,
int hashIndex,
AbstractHashMap.HashEntry previous)
Removes a mapping from the map.
|
protected void |
reuseEntry(AbstractHashMap.HashEntry entry,
int hashIndex,
int hashCode,
Object key,
Object value)
Reuses an existing key-value mapping, storing completely new data.
|
int |
size()
Gets the size of the map.
|
String |
toString()
Gets the map as a String.
|
protected void |
updateEntry(AbstractHashMap.HashEntry entry,
Object newValue)
Updates an existing key-value mapping to change the value.
|
Collection |
values()
Gets the values view of the map.
|
finalize, getClass, notify, notifyAll, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllprotected static final String NO_NEXT_ENTRY
Exception messages.protected static final String NO_PREVIOUS_ENTRY
protected static final String REMOVE_INVALID
protected static final String GETKEY_INVALID
protected static final String GETVALUE_INVALID
protected static final String SETVALUE_INVALID
protected static final int DEFAULT_CAPACITY
protected static final int DEFAULT_THRESHOLD
protected static final float DEFAULT_LOAD_FACTOR
protected static final int MAXIMUM_CAPACITY
protected static final Object NULL
protected transient float loadFactor
protected transient int size
protected transient AbstractHashMap.HashEntry<K,V>[] data
protected transient int threshold
protected transient int modCount
protected transient AbstractHashMap.EntrySet entrySet
protected transient AbstractHashMap.KeySet keySet
protected transient AbstractHashMap.Values values
protected AbstractHashMap()
protected AbstractHashMap(int initialCapacity)
initialCapacity - the initial capacityIllegalArgumentException - if the initial capacity is less than oneprotected AbstractHashMap(int initialCapacity,
float loadFactor)
initialCapacity - the initial capacityloadFactor - the load factorIllegalArgumentException - if the initial capacity is less than oneIllegalArgumentException - if the load factor is less than or equal to zeroprotected AbstractHashMap(int initialCapacity,
float loadFactor,
int threshold)
initialCapacity - the initial capacity, must be a power of twoloadFactor - the load factor, must be > 0.0f and generally < 1.0fthreshold - the threshold, must be sensibleprotected AbstractHashMap(@NotNull Map map)
map - the map to copyNullPointerException - if the map is nullprotected void init()
public int size()
public boolean isEmpty()
public boolean containsKey(Object key)
containsKey in interface Map<K,V>containsKey in class AbstractMap<K,V>key - the key to search forpublic boolean containsValue(Object value)
containsValue in interface Map<K,V>containsValue in class AbstractMap<K,V>value - the value to search forpublic void putAll(Map map)
This implementation iterates around the specified map and
uses put(Object, Object).
putAll in interface Map<K,V>putAll in class AbstractMap<K,V>map - the map to addNullPointerException - if the map is nullpublic void clear()
protected Object convertKey(Object key)
The reverse conversion can be changed, if required, by overriding the getKey() method in the hash entry.
key - the key convertprotected int hash(Object key)
key - the key to get a hash code forprotected boolean isEqualKey(Object key1, Object key2)
key1 - the first key to compare passed in from outsidekey2 - the second key extracted from the entry via entry.keyprotected boolean isEqualValue(Object value1, Object value2)
value1 - the first value to compare passed in from outsidevalue2 - the second value extracted from the entry via getValue()protected int hashIndex(int hashCode,
int dataSize)
hashCode - the hash code to usedataSize - the size of the data to pick a bucket fromprotected AbstractHashMap.HashEntry getEntry(Object key)
This method exists for subclasses that may need to perform a multi-step process accessing the entry. The public methods in this class don't use this method to gain a small performance boost.
key - the keyprotected void updateEntry(AbstractHashMap.HashEntry entry, Object newValue)
This implementation calls setValue() on the entry.
Subclasses could override to handle changes to the map.
entry - the entry to updatenewValue - the new value to storeprotected void reuseEntry(AbstractHashMap.HashEntry entry, int hashIndex, int hashCode, Object key, Object value)
This implementation sets all the data fields on the entry. Subclasses could populate additional entry fields.
entry - the entry to update, not nullhashIndex - the index in the data arrayhashCode - the hash code of the key to addkey - the key to addvalue - the value to addprotected void addMapping(int hashIndex,
int hashCode,
Object key,
Object value)
This implementation calls createEntry(), addEntry()
and checkCapacity().
It also handles changes to modCount and size.
Subclasses could override to fully control adds to the map.
hashIndex - the index into the data array to store athashCode - the hash code of the key to addkey - the key to addvalue - the value to addprotected AbstractHashMap.HashEntry createEntry(AbstractHashMap.HashEntry next, int hashCode, Object key, Object value)
This implementation creates a new HashEntry instance. Subclasses can override this to return a different storage class, or implement caching.
next - the next entry in sequencehashCode - the hash code to usekey - the key to storevalue - the value to storeprotected void addEntry(AbstractHashMap.HashEntry entry, int hashIndex)
This implementation adds the entry to the data storage table. Subclasses could override to handle changes to the map.
entry - the entry to addhashIndex - the index into the data array to store atprotected void removeMapping(AbstractHashMap.HashEntry entry, int hashIndex, AbstractHashMap.HashEntry previous)
This implementation calls removeEntry() and destroyEntry().
It also handles changes to modCount and size.
Subclasses could override to fully control removals from the map.
entry - the entry to removehashIndex - the index into the data structureprevious - the previous entry in the chainprotected void removeEntry(AbstractHashMap.HashEntry entry, int hashIndex, AbstractHashMap.HashEntry previous)
This implementation removes the entry from the data storage table. The size is not updated. Subclasses could override to handle changes to the map.
entry - the entry to removehashIndex - the index into the data structureprevious - the previous entry in the chainprotected void destroyEntry(AbstractHashMap.HashEntry entry)
This implementation prepares the HashEntry for garbage collection. Subclasses can override this to implement caching (override clear as well).
entry - the entry to destroyprotected void checkCapacity()
This implementation uses the threshold to check if the map needs enlarging
protected void ensureCapacity(int newCapacity)
newCapacity - the new capacity of the array (a power of two, less or equal to max)protected int calculateNewCapacity(int proposedCapacity)
proposedCapacity - the proposed capacityprotected int calculateThreshold(int newCapacity,
float factor)
newCapacity - the new capacityfactor - the load factorprotected AbstractHashMap.HashEntry entryNext(AbstractHashMap.HashEntry entry)
next field from a HashEntry.
Used in subclasses that have no visibility of the field.entry - the entry to query, must not be nullnext field of the entryNullPointerException - if the entry is nullprotected int entryHashCode(AbstractHashMap.HashEntry entry)
hashCode field from a HashEntry.
Used in subclasses that have no visibility of the field.entry - the entry to query, must not be nullhashCode field of the entryNullPointerException - if the entry is nullprotected Object entryKey(AbstractHashMap.HashEntry entry)
key field from a HashEntry.
Used in subclasses that have no visibility of the field.entry - the entry to query, must not be nullkey field of the entryNullPointerException - if the entry is nullprotected Object entryValue(AbstractHashMap.HashEntry entry)
value field from a HashEntry.
Used in subclasses that have no visibility of the field.entry - the entry to query, must not be nullvalue field of the entryNullPointerException - if the entry is nullpublic MapIterator mapIterator()
A MapIterator returns the keys in the map. It also provides convenient methods to get the key and value, and set the value. It avoids the need to create an entrySet/keySet/values object. It also avoids creating the Map.Entry object.
mapIterator in interface IterableMap<K,V>public Set entrySet()
mapIterator().protected Iterator createEntrySetIterator()
public Set keySet()
mapIterator().protected Iterator<K> createKeySetIterator()
public Collection values()
mapIterator().protected Iterator createValuesIterator()
protected void doWriteObject(ObjectOutputStream out) throws IOException
put() is used.
Serialization is not one of the JDK's nicest topics. Normal serialization will
initialise the superclass before the subclass. Sometimes however, this isn't
what you want, as in this case the put() method on read can be
affected by subclass state.
The solution adopted here is to serialize the state data of this class in
this protected method. This method must be called by the
writeObject() of the first serializable subclass.
Subclasses may override if they have a specific field that must be present on read before this implementation will work. Generally, the read determines what must be serialized here, if anything.
out - the output streamIOException - if IO operation failsprotected void doReadObject(ObjectInputStream in) throws IOException, ClassNotFoundException
put() is used.
Serialization is not one of the JDK's nicest topics. Normal serialization will
initialise the superclass before the subclass. Sometimes however, this isn't
what you want, as in this case the put() method on read can be
affected by subclass state.
The solution adopted here is to deserialize the state data of this class in
this protected method. This method must be called by the
readObject() of the first serializable subclass.
Subclasses may override if the subclass has a specific field that must be present
before put() or calculateThreshold() will work correctly.
in - the input streamIOException - if IO operation failsClassNotFoundException - if unable to resolve object classprotected Object clone()
To implement clone(), a subclass must implement the
Cloneable interface and make this method public.
clone in class AbstractMap<K,V>public boolean equals(Object obj)
public int hashCode()
public String toString()
toString in class AbstractMap<K,V>Copyright © 2020. All rights reserved.