Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.map

public Class AbstractHashedMap<K, V>

extends AbstractMap<K, V>
implements IterableMap<K, V>
Class Inheritance
All Implemented Interfaces
org.apache.commons.collections4.IterableMap, org.apache.commons.collections4.IterableGet, org.apache.commons.collections4.Get, org.apache.commons.collections4.Put, java.util.Map
Known Direct Subclasses
org.apache.commons.collections4.map.AbstractLinkedMap, org.apache.commons.collections4.map.AbstractReferenceMap, org.apache.commons.collections4.map.CaseInsensitiveMap, org.apache.commons.collections4.map.HashedMap
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, java.util.AbstractCollection, .AbstractMap, .AbstractSet, .Collection, .ConcurrentModificationException, .Iterator, .Map, .NoSuchElementException, .Set, org.apache.commons.collections4.IterableMap, .KeyValue, .MapIterator, org.apache.commons.collections4.iterators.EmptyIterator, .EmptyMapIterator

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 v5.0.

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
AbstractHashedMap.EntrySet<K, V>

EntrySet implementation.

protected static class
AbstractHashedMap.EntrySetIterator<K, V>

EntrySet iterator.

protected static class
AbstractHashedMap.HashEntry<K, V>

HashEntry used to store the data.

protected abstract static class
AbstractHashedMap.HashIterator<K, V>

Base Iterator

protected static class
AbstractHashedMap.HashMapIterator<K, V>

MapIterator implementation.

protected static class
AbstractHashedMap.KeySet<K>

KeySet implementation.

protected static class
protected static class
AbstractHashedMap.Values<V>

Values implementation.

protected static class

Field Summary

Modifier and TypeField and Description
pack-priv transient AbstractHashedMap.HashEntry<K, V>[]
data

Map entries

protected static final int
DEFAULT_CAPACITY

The default capacity to use

protected static final float
DEFAULT_LOAD_FACTOR

The default load factor to use

protected static final int
DEFAULT_THRESHOLD

The default threshold to use

pack-priv transient AbstractHashedMap.EntrySet<K, V>
entrySet

Entry set

protected static final String
protected static final String
pack-priv transient AbstractHashedMap.KeySet<K>
pack-priv transient float
loadFactor

Load factor, normally 0.75

protected static final int
MAXIMUM_CAPACITY

The maximum capacity allowed

pack-priv transient int
modCount

Modification count for iterators

protected static final String
protected static final String
protected static final Object
NULL

An object for masking null

protected static final String
protected static final String
pack-priv transient int
size

The size of the map

pack-priv transient int
threshold

Size at which to rehash

pack-priv transient AbstractHashedMap.Values<V>

Constructor Summary

AccessConstructor and Description
protected
AbstractHashedMap()

Constructor only used in deserialization, do not use otherwise.

protected
AbstractHashedMap(final int
the initial capacity, must be a power of two
initialCapacity
,
final float
the load factor, must be > 0.0f and generally < 1.0f
loadFactor
,
final int
the threshold, must be sensible
threshold
)

Constructor which performs no validation on the passed in parameters.

protected
AbstractHashedMap(final int
the initial capacity
initialCapacity
)

Constructs a new, empty map with the specified initial capacity and default load factor.

protected
AbstractHashedMap(int
the initial capacity
initialCapacity
,
final float
the load factor
loadFactor
)

Constructs a new, empty map with the specified initial capacity and load factor.

protected
AbstractHashedMap(final Map<? extends K, ? extends V>
the map to copy
map
)

Constructor copying elements from another map.

Method Summary

Modifier and TypeMethod and Description
private void
_putAll(final Map<? extends K, ? extends V>
the map to add
map
)

Puts all the values from the specified map into this map.

protected void
addEntry(final AbstractHashedMap.HashEntry<K, V>
the entry to add
entry
,
final int
the index into the data array to store at
hashIndex
)

Adds an entry into this map.

protected void
addMapping(final int
the index into the data array to store at
hashIndex
,
final int
the hash code of the key to add
hashCode
,
final K
the key to add
key
,
final V
the value to add
value
)

Adds a new key-value mapping into this map.

protected int

Returns:

the normalized new capacity
calculateNewCapacity
(final int
the proposed capacity
proposedCapacity
)

Calculates the new capacity of the map.

protected int

Returns:

the new resize threshold
calculateThreshold
(final int
the new capacity
newCapacity
,
final float
the load factor
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.

public void
clear()

Overrides java.util.AbstractMap.clear.

Implements java.util.Map.clear, org.apache.commons.collections4.Put.clear.

Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.
protected AbstractHashedMap<K, V>

Returns:

a shallow clone
clone
()

Overrides java.util.AbstractMap.clone.

Clones the map without cloning the keys or values.
public boolean

Returns:

true if the map contains the key
containsKey
(Object
the key to search for
key
)

Overrides java.util.AbstractMap.containsKey.

Implements java.util.Map.containsKey, org.apache.commons.collections4.Get.containsKey.

Checks whether the map contains the specified key.
public boolean

Returns:

true if the map contains the value
containsValue
(final Object
the value to search for
value
)

Overrides java.util.AbstractMap.containsValue.

Implements java.util.Map.containsValue, org.apache.commons.collections4.Get.containsValue.

Checks whether the map contains the specified value.
protected Object

Returns:

the converted key
convertKey
(final Object
the key convert
key
)

Converts input keys to another object for storage in the map.

protected AbstractHashedMap.HashEntry<K, V>

Returns:

the newly created entry
createEntry
(final AbstractHashedMap.HashEntry<K, V>
the next entry in sequence
next
,
final int
the hash code to use
hashCode
,
final K
the key to store
key
,
final V
the value to store
value
)

Creates an entry to store the key-value data.

protected Iterator<Map.Entry<K, V>>

Returns:

the entrySet iterator
createEntrySetIterator
()

Creates an entry set iterator.

protected Iterator<K>

Returns:

the keySet iterator
createKeySetIterator
()

Creates a key set iterator.

protected Iterator<V>

Returns:

the values iterator
createValuesIterator
()

Creates a values iterator.

protected void
destroyEntry(final AbstractHashedMap.HashEntry<K, V>
the entry to destroy
entry
)

Kills an entry ready for the garbage collector.

protected void
doReadObject(final ObjectInputStream
the input stream
in
)

Reads the map data from the stream.

protected void
doWriteObject(final ObjectOutputStream
the output stream
out
)

Writes the map data to the stream.

protected void
ensureCapacity(final int
the new capacity of the array (a power of two, less or equal to max)
newCapacity
)

Changes the size of the data structure to the capacity proposed.

protected int

Returns:

the hashCode field of the entry
entryHashCode
(final AbstractHashedMap.HashEntry<K, V>
the entry to query, must not be null
entry
)

Gets the hashCode field from a HashEntry.

protected K

Returns:

the key field of the entry
entryKey
(final AbstractHashedMap.HashEntry<K, V>
the entry to query, must not be null
entry
)

Gets the key field from a HashEntry.

protected AbstractHashedMap.HashEntry<K, V>

Returns:

the next field of the entry
entryNext
(final AbstractHashedMap.HashEntry<K, V>
the entry to query, must not be null
entry
)

Gets the next field from a HashEntry.

public Set<Map.Entry<K, V>>

Returns:

the entrySet view
entrySet
()

Implements abstract java.util.AbstractMap.entrySet.

Implements java.util.Map.entrySet, org.apache.commons.collections4.Get.entrySet.

Gets the entrySet view of the map.
protected V

Returns:

the value field of the entry
entryValue
(final AbstractHashedMap.HashEntry<K, V>
the entry to query, must not be null
entry
)

Gets the value field from a HashEntry.

public boolean

Returns:

true if equal
equals
(final Object
the object to compare to
obj
)

Overrides java.util.AbstractMap.equals.

Implements java.util.Map.equals.

Compares this map with another.
public V

Returns:

the mapped value, null if no match
get
(Object
the key
key
)

Overrides java.util.AbstractMap.get.

Implements java.util.Map.get, org.apache.commons.collections4.Get.get.

Gets the value mapped to the key specified.
protected AbstractHashedMap.HashEntry<K, V>

Returns:

the entry, null if no match
getEntry
(Object
the key
key
)

Gets the entry mapped to the key specified.

protected int

Returns:

the hash code
hash
(final Object
the key to get a hash code for
key
)

Gets the hash code for the key specified.

public int

Returns:

the hash code defined in the Map interface
hashCode
()

Overrides java.util.AbstractMap.hashCode.

Implements java.util.Map.hashCode.

Gets the standard Map hashCode.
protected int

Returns:

the bucket index
hashIndex
(final int
the hash code to use
hashCode
,
final int
the size of the data to pick a bucket from
dataSize
)

Gets the index into the data storage for the hashCode specified.

protected void
init()

Initialise subclasses during construction, cloning or deserialization.

public boolean

Returns:

true if the map is currently size zero
isEmpty
()

Overrides java.util.AbstractMap.isEmpty.

Implements java.util.Map.isEmpty, org.apache.commons.collections4.Get.isEmpty.

Checks whether the map is currently empty.
protected boolean

Returns:

true if equal
isEqualKey
(final Object
the first key to compare passed in from outside
key1
,
final Object
the second key extracted from the entry via entry.key
key2
)

Compares two keys, in internal converted form, to see if they are equal.

protected boolean

Returns:

true if equal
isEqualValue
(final Object
the first value to compare passed in from outside
value1
,
final Object
the second value extracted from the entry via getValue()
value2
)

Compares two values, in external form, to see if they are equal.

public Set<K>

Returns:

the keySet view
keySet
()

Overrides java.util.AbstractMap.keySet.

Implements java.util.Map.keySet, org.apache.commons.collections4.Get.keySet.

Gets the keySet view of the map.
public MapIterator<K, V>

Returns:

the map iterator
mapIterator
()

Implements org.apache.commons.collections4.IterableGet.mapIterator.

Gets an iterator over the map.
public V

Returns:

the value previously mapped to this key, null if none
put
(final K
the key to add
key
,
final V
the value to add
value
)

Overrides java.util.AbstractMap.put.

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

Puts a key-value mapping into this map.
public void
putAll(final Map<? extends K, ? extends V>
the map to add
map
)

Overrides java.util.AbstractMap.putAll.

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

Puts all the values from the specified map into this map.
public V

Returns:

the value mapped to the removed key, null if key not in map
remove
(Object
the mapping to remove
key
)

Overrides java.util.AbstractMap.remove.

Implements java.util.Map.remove, org.apache.commons.collections4.Get.remove.

Removes the specified mapping from this map.
protected void
removeEntry(final AbstractHashedMap.HashEntry<K, V>
the entry to remove
entry
,
final int
the index into the data structure
hashIndex
,
final AbstractHashedMap.HashEntry<K, V>
the previous entry in the chain
previous
)

Removes an entry from the chain stored in a particular index.

protected void
removeMapping(final AbstractHashedMap.HashEntry<K, V>
the entry to remove
entry
,
final int
the index into the data structure
hashIndex
,
final AbstractHashedMap.HashEntry<K, V>
the previous entry in the chain
previous
)

Removes a mapping from the map.

protected void
reuseEntry(final AbstractHashedMap.HashEntry<K, V>
the entry to update, not null
entry
,
final int
the index in the data array
hashIndex
,
final int
the hash code of the key to add
hashCode
,
final K
the key to add
key
,
final V
the value to add
value
)

Reuses an existing key-value mapping, storing completely new data.

public int

Returns:

the size
size
()

Overrides java.util.AbstractMap.size.

Implements java.util.Map.size, org.apache.commons.collections4.Get.size.

Gets the size of the map.
public String

Returns:

a string version of the map
toString
()

Overrides java.util.AbstractMap.toString.

Gets the map as a String.
protected void
updateEntry(final AbstractHashedMap.HashEntry<K, V>
the entry to update
entry
,
final V
the new value to store
newValue
)

Updates an existing key-value mapping to change the value.

public Collection<V>

Returns:

the values view
values
()

Overrides java.util.AbstractMap.values.

Implements java.util.Map.values, org.apache.commons.collections4.Get.values.

Gets the values view of the map.