K - the key typeV - the value typepublic class HashMap<K,V>
extends java.lang.Object
java.util.HashMap
in order to release dependencies during instrumentation.| Modifier and Type | Class and Description |
|---|---|
static class |
HashMap.Entry<K,V>
Implements a bucket entry.
|
private class |
HashMap.KeyIterator<I>
A specific iterator for the keys.
|
private class |
HashMap.MapIterator<I>
Implements a generic iterator.
|
| Modifier and Type | Field and Description |
|---|---|
private static int |
DEFAULT_INITIAL_CAPACITY
The default initial capacity - MUST be a power of two.
|
private static float |
DEFAULT_LOAD_FACTOR
The load factor used when none specified in constructor.
|
private int |
keySize
The number of keys contained in this map.
|
private float |
loadFactor
The load factor for the hash table.
|
private static int |
MAXIMUM_CAPACITY
The maximum capacity, used if a higher value is implicitly specified
by either of the constructors with arguments.
|
private int |
size
The number of key-value mappings contained in this map.
|
private HashMap.Entry[] |
table
The table, resized as necessary.
|
private int |
threshold
The next size value at which to resize (capacity * load factor).
|
| Constructor and Description |
|---|
HashMap()
Constructs an empty HashMap with the default initial capacity
(16) and the default load factor (0.75).
|
HashMap(int initialCapacity)
Constructs an empty HashMap with the specified initial
capacity and the default load factor (0.75).
|
HashMap(int initialCapacity,
float loadFactor)
Constructs an empty HashMap with the specified initial
capacity and load factor.
|
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
addEntry(int hash,
K key,
V value,
int bucketIndex)
Adds a new entry with the specified key, value and hash code to
the specified bucket.
|
void |
clear()
Removes all of the mappings from this map.
|
boolean |
containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the
specified key.
|
java.lang.Iterable<HashMap.Entry<K,V>> |
entries()
Returns an iterator over the entries stored in this map.
|
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) HashMap.Entry<K,V> |
getEntry(java.lang.Object key)
Returns the entry associated with the specified key in the
HashMap.
|
private V |
getForNullKey()
Offloaded version of get() to look up null keys.
|
(package private) static int |
hash(int key)
Applies a supplemental hash function to a given hashCode, which
defends against poor quality hash functions.
|
(package private) static int |
indexFor(int hash,
int length)
Returns index for hash code h.
|
boolean |
isEmpty()
Returns true if this map contains no key-value mappings.
|
java.lang.Iterable<K> |
keys()
Returns an iterator over the keys stored in this map.
|
int |
keySize()
Returns the number of keys in this map.
|
void |
keysToArray(K[] array)
Stores all keys of this map into
array. |
HashMap<K,V> |
keysToMap()
Stores all keys of this map into the resulting map.
|
V |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
void |
putAll(HashMap<K,V> data)
Puts all elements in
data into this hash map. |
void |
putAllKeys(HashMap<K,?> data)
Puts all elements in
data into this hash map. |
private V |
putForNullKey(V value)
Offloaded version of put for null keys.
|
V |
remove(java.lang.Object key)
Removes the mapping of
key and returns its value. |
(package private) void |
resize(int newCapacity)
Rehashes the contents of this map into a new array with a
larger capacity.
|
int |
size()
Returns the number of key-value mappings in this map.
|
java.lang.String |
toString()
Returns a textual representation of this map.
|
(package private) void |
transfer(HashMap.Entry[] newTable)
Transfers all entries from current table to newTable.
|
java.lang.Iterable<V> |
values()
Returns an iterator over the values stored in this map.
|
private static final int DEFAULT_INITIAL_CAPACITY
private static final int MAXIMUM_CAPACITY
private static final float DEFAULT_LOAD_FACTOR
private transient HashMap.Entry[] table
private transient int size
private transient int keySize
private int threshold
private final float loadFactor
public HashMap(int initialCapacity,
float loadFactor)
initialCapacity - the initial capacityloadFactor - the load factorjava.lang.IllegalArgumentException - if the initial capacity is negative
or the load factor is nonpositivepublic HashMap(int initialCapacity)
initialCapacity - the initial capacity.java.lang.IllegalArgumentException - if the initial capacity is negative.public HashMap()
public boolean containsKey(java.lang.Object key)
key - The key whose presence in this map is to be testedstatic int hash(int key)
key - the key to hashfinal HashMap.Entry<K,V> getEntry(java.lang.Object key)
key - the key to return the stored object forstatic int indexFor(int hash,
int length)
hash - the hash codelength - the length of the tablepublic V put(K key, V value)
key - key with which the specified value is to be associatedvalue - value to be associated with the specified keyprivate V putForNullKey(V value)
value - the valuevoid addEntry(int hash,
K key,
V value,
int bucketIndex)
hash - the hash codekey - the key to addvalue - the value to addbucketIndex - the target bucket indexvoid resize(int newCapacity)
newCapacity - the new capacity, MUST be a power of two;
must be greater than current capacity unless current
capacity is MAXIMUM_CAPACITY (in which case value
is irrelevant).void transfer(HashMap.Entry[] newTable)
newTable - the target tablepublic V get(java.lang.Object key)
null if this map contains no mapping for the key.key - the key to search forput(Object, Object)public V remove(java.lang.Object key)
key and returns its value.key - the key to remove the entrykeyprivate V getForNullKey()
public int size()
public int keySize()
public boolean isEmpty()
public java.lang.Iterable<V> values()
public java.lang.Iterable<HashMap.Entry<K,V>> entries()
public java.lang.Iterable<K> keys()
public void clear()
public java.lang.String toString()
toString in class java.lang.Objectpublic void keysToArray(K[] array)
array.array - the array to be modified as a side effect, nothing will
happen if array == null, if array size is less
than keySize() then only array.length keys will
be copiedpublic HashMap<K,V> keysToMap()
public void putAll(HashMap<K,V> data)
data into this hash map.data - the elements to process