java.lang.Object | ||
↳ | java.util.AbstractMap<K, V> | |
↳ | org.apache.commons.collections.map.AbstractHashedMap |
Known Direct Subclasses |
Known Indirect Subclasses |
An abstract implementation of a hash-based map which provides numerous points for subclasses to override.
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.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
AbstractHashedMap.EntrySet | EntrySet implementation. | ||||||||||
AbstractHashedMap.EntrySetIterator | EntrySet iterator. | ||||||||||
AbstractHashedMap.HashEntry | HashEntry used to store the data. | ||||||||||
AbstractHashedMap.HashIterator | Base Iterator | ||||||||||
AbstractHashedMap.HashMapIterator | MapIterator implementation. | ||||||||||
AbstractHashedMap.KeySet | KeySet implementation. | ||||||||||
AbstractHashedMap.KeySetIterator | KeySet iterator. | ||||||||||
AbstractHashedMap.Values | Values implementation. | ||||||||||
AbstractHashedMap.ValuesIterator | Values iterator. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | DEFAULT_CAPACITY | The default capacity to use | |||||||||
float | DEFAULT_LOAD_FACTOR | The default load factor to use | |||||||||
int | DEFAULT_THRESHOLD | The default threshold to use | |||||||||
String | GETKEY_INVALID | ||||||||||
String | GETVALUE_INVALID | ||||||||||
int | MAXIMUM_CAPACITY | The maximum capacity allowed | |||||||||
String | NO_NEXT_ENTRY | ||||||||||
String | NO_PREVIOUS_ENTRY | ||||||||||
String | REMOVE_INVALID | ||||||||||
String | SETVALUE_INVALID |
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
NULL | An object for masking null | ||||||||||
data | Map entries | ||||||||||
entrySet | Entry set | ||||||||||
keySet | Key set | ||||||||||
loadFactor | Load factor, normally 0.75 | ||||||||||
modCount | Modification count for iterators | ||||||||||
size | The size of the map | ||||||||||
threshold | Size at which to rehash | ||||||||||
values | Values |
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructor only used in deserialization, do not use otherwise.
| |||||||||||
Constructor which performs no validation on the passed in parameters.
| |||||||||||
Constructs a new, empty map with the specified initial capacity and
default load factor.
| |||||||||||
Constructs a new, empty map with the specified initial capacity and
load factor.
| |||||||||||
Constructor copying elements from another map.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Clears the map, resetting the size to zero and nullifying references
to avoid garbage collection issues.
| |||||||||||
Checks whether the map contains the specified key.
| |||||||||||
Checks whether the map contains the specified value.
| |||||||||||
Gets the entrySet view of the map.
| |||||||||||
Compares this map with another.
| |||||||||||
Gets the value mapped to the key specified.
| |||||||||||
Gets the standard Map hashCode.
| |||||||||||
Checks whether the map is currently empty.
| |||||||||||
Gets the keySet view of the map.
| |||||||||||
Gets an iterator over the map.
| |||||||||||
Puts a key-value mapping into this map.
| |||||||||||
Puts all the values from the specified map into this map.
| |||||||||||
Removes the specified mapping from this map.
| |||||||||||
Gets the size of the map.
| |||||||||||
Gets the map as a String.
| |||||||||||
Gets the values view of the map.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds an entry into this map.
| |||||||||||
Adds a new key-value mapping into this map.
| |||||||||||
Calculates the new capacity of the map.
| |||||||||||
Calculates the new threshold of the map, where it will be resized.
| |||||||||||
Checks the capacity of the map and enlarges it if necessary.
| |||||||||||
Clones the map without cloning the keys or values.
| |||||||||||
Converts input keys to another object for storage in the map.
| |||||||||||
Creates an entry to store the key-value data.
| |||||||||||
Creates an entry set iterator.
| |||||||||||
Creates a key set iterator.
| |||||||||||
Creates a values iterator.
| |||||||||||
Kills an entry ready for the garbage collector.
| |||||||||||
Reads the map data from the stream.
| |||||||||||
Writes the map data to the stream.
| |||||||||||
Changes the size of the data structure to the capacity proposed.
| |||||||||||
Gets the
hashCode field from a HashEntry . | |||||||||||
Gets the
key field from a HashEntry . | |||||||||||
Gets the
next field from a HashEntry . | |||||||||||
Gets the
value field from a HashEntry . | |||||||||||
Gets the entry mapped to the key specified.
| |||||||||||
Gets the hash code for the key specified.
| |||||||||||
Gets the index into the data storage for the hashCode specified.
| |||||||||||
Initialise subclasses during construction, cloning or deserialization.
| |||||||||||
Compares two keys, in internal converted form, to see if they are equal.
| |||||||||||
Compares two values, in external form, to see if they are equal.
| |||||||||||
Removes an entry from the chain stored in a particular index.
| |||||||||||
Removes a mapping from the map.
| |||||||||||
Reuses an existing key-value mapping, storing completely new data.
| |||||||||||
Updates an existing key-value mapping to change the value.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.util.AbstractMap
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
java.util.Map
| |||||||||||
From interface
org.apache.commons.collections.IterableMap
|
The default capacity to use
The default load factor to use
The default threshold to use
The maximum capacity allowed
Load factor, normally 0.75
Modification count for iterators
The size of the map
Size at which to rehash
Constructor only used in deserialization, do not use otherwise.
Constructor which performs no validation on the passed in parameters.
initialCapacity | the initial capacity, must be a power of two |
---|---|
loadFactor | the load factor, must be > 0.0f and generally < 1.0f |
threshold | the threshold, must be sensible |
Constructs a new, empty map with the specified initial capacity and default load factor.
initialCapacity | the initial capacity |
---|
IllegalArgumentException | if the initial capacity is less than one |
---|
Constructs a new, empty map with the specified initial capacity and load factor.
initialCapacity | the initial capacity |
---|---|
loadFactor | the load factor |
IllegalArgumentException | if the initial capacity is less than one |
---|---|
IllegalArgumentException | if the load factor is less than or equal to zero |
Constructor copying elements from another map.
map | the map to copy |
---|
NullPointerException | if the map is null |
---|
Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.
Checks whether the map contains the specified key.
key | the key to search for |
---|
Checks whether the map contains the specified value.
value | the value to search for |
---|
Gets the entrySet view of the map.
Changes made to the view affect this map.
To simply iterate through the entries, use mapIterator()
.
Compares this map with another.
obj | the object to compare to |
---|
Gets the value mapped to the key specified.
key | the key |
---|
Gets the standard Map hashCode.
Checks whether the map is currently empty.
Gets the keySet view of the map.
Changes made to the view affect this map.
To simply iterate through the keys, use mapIterator()
.
Gets an iterator over the map. Changes made to the iterator affect this map.
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.
Puts a key-value mapping into this map.
key | the key to add |
---|---|
value | the value to add |
Puts all the values from the specified map into this map.
This implementation iterates around the specified map and
uses put(Object, Object)
.
map | the map to add |
---|
NullPointerException | if the map is null |
---|
Removes the specified mapping from this map.
key | the mapping to remove |
---|
Gets the size of the map.
Gets the values view of the map.
Changes made to the view affect this map.
To simply iterate through the values, use mapIterator()
.
Adds an entry into this map.
This implementation adds the entry to the data storage table. Subclasses could override to handle changes to the map.
entry | the entry to add |
---|---|
hashIndex | the index into the data array to store at |
Adds a new key-value mapping into this map.
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 at |
---|---|
hashCode | the hash code of the key to add |
key | the key to add |
value | the value to add |
Calculates the new capacity of the map. This implementation normalizes the capacity to a power of two.
proposedCapacity | the proposed capacity |
---|
Calculates the new threshold of the map, where it will be resized. This implementation uses the load factor.
newCapacity | the new capacity |
---|---|
factor | the load factor |
Checks the capacity of the map and enlarges it if necessary.
This implementation uses the threshold to check if the map needs enlarging
Clones the map without cloning the keys or values.
To implement clone()
, a subclass must implement the
Cloneable
interface and make this method public.
Converts input keys to another object for storage in the map. This implementation masks nulls. Subclasses can override this to perform alternate key conversions.
The reverse conversion can be changed, if required, by overriding the getKey() method in the hash entry.
key | the key convert |
---|
Creates an entry to store the key-value data.
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 sequence |
---|---|
hashCode | the hash code to use |
key | the key to store |
value | the value to store |
Creates an entry set iterator. Subclasses can override this to return iterators with different properties.
Creates a key set iterator. Subclasses can override this to return iterators with different properties.
Creates a values iterator. Subclasses can override this to return iterators with different properties.
Kills an entry ready for the garbage collector.
This implementation prepares the HashEntry for garbage collection. Subclasses can override this to implement caching (override clear as well).
entry | the entry to destroy |
---|
Reads the map data from the stream. This method must be overridden if a
subclass must be setup before 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 stream |
---|
Writes the map data to the stream. This method must be overridden if a
subclass must be setup before 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 stream |
---|
IOException |
---|
Changes the size of the data structure to the capacity proposed.
newCapacity | the new capacity of the array (a power of two, less or equal to max) |
---|
Gets the hashCode
field from a HashEntry
.
Used in subclasses that have no visibility of the field.
entry | the entry to query, must not be null |
---|
hashCode
field of the entryNullPointerException | if the entry is null |
---|
Gets the key
field from a HashEntry
.
Used in subclasses that have no visibility of the field.
entry | the entry to query, must not be null |
---|
key
field of the entryNullPointerException | if the entry is null |
---|
Gets the next
field from a HashEntry
.
Used in subclasses that have no visibility of the field.
entry | the entry to query, must not be null |
---|
next
field of the entryNullPointerException | if the entry is null |
---|
Gets the value
field from a HashEntry
.
Used in subclasses that have no visibility of the field.
entry | the entry to query, must not be null |
---|
value
field of the entryNullPointerException | if the entry is null |
---|
Gets the entry mapped to the key specified.
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 key |
---|
Gets the hash code for the key specified. This implementation uses the additional hashing routine from JDK1.4. Subclasses can override this to return alternate hash codes.
key | the key to get a hash code for |
---|
Gets the index into the data storage for the hashCode specified. This implementation uses the least significant bits of the hashCode. Subclasses can override this to return alternate bucketing.
hashCode | the hash code to use |
---|---|
dataSize | the size of the data to pick a bucket from |
Initialise subclasses during construction, cloning or deserialization.
Compares two keys, in internal converted form, to see if they are equal. This implementation uses the equals method and assumes neither key is null. Subclasses can override this to match differently.
key1 | the first key to compare passed in from outside |
---|---|
key2 | the second key extracted from the entry via entry.key |
Compares two values, in external form, to see if they are equal. This implementation uses the equals method and assumes neither value is null. Subclasses can override this to match differently.
value1 | the first value to compare passed in from outside |
---|---|
value2 | the second value extracted from the entry via getValue() |
Removes an entry from the chain stored in a particular index.
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 remove |
---|---|
hashIndex | the index into the data structure |
previous | the previous entry in the chain |
Removes a mapping from the map.
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 remove |
---|---|
hashIndex | the index into the data structure |
previous | the previous entry in the chain |
Reuses an existing key-value mapping, storing completely new data.
This implementation sets all the data fields on the entry. Subclasses could populate additional entry fields.
entry | the entry to update, not null |
---|---|
hashIndex | the index in the data array |
hashCode | the hash code of the key to add |
key | the key to add |
value | the value to add |
Updates an existing key-value mapping to change the value.
This implementation calls setValue()
on the entry.
Subclasses could override to handle changes to the map.
entry | the entry to update |
---|---|
newValue | the new value to store |