概述
- 線程非安全团滥,并且允許key與value都為null值颓影,HashTable與之相反,為線程安全盾鳞,key與value都不允許null值犬性。
- put、get操作的時(shí)間復(fù)雜度為O(1)腾仅。
- 不保證其內(nèi)部元素的順序乒裆,而且隨著時(shí)間的推移,同一元素的位置也可能改變(resize的情況)
- 遍歷其集合視角的時(shí)間復(fù)雜度與其容量(capacity攒砖,槽的個(gè)數(shù))和現(xiàn)有元素的大懈淄谩(entry的個(gè)數(shù))成正比
- Map m = Collections.synchronizedMap(new HashMap(...)); 通過這種方式可以得到一個(gè)線程安全的map日裙。
數(shù)據(jù)結(jié)構(gòu)
哈希表:數(shù)組+鏈表
markdown-img-paste-20161128182007235.png
定義
簽名
public class HashMap<K,V>
extends AbstractMap<K,V> //AbstractMap類提供Map接口的基本實(shí)現(xiàn)
implements Map<K,V>, Cloneable, Serializable//Map接口定義了鍵映射到值的規(guī)則
哈希函數(shù)的設(shè)計(jì)
final int hash(Object k) {
int h = hashSeed;
if (0 != h && k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode();
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
get操作
public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
private V getForNullKey() {
if (size == 0) {
return null;
}
// Null keys map to index 0,遍歷單向鏈表
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null)
return e.value;
}
return null;
}
final Entry<K,V> getEntry(Object key) {
// 根據(jù)key尋找entry,類似于給下表惰蜜,在數(shù)組中尋找
if (size == 0) {
return null;
}
int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
// 相等條件:hash值相等且key相等(==或者equal相等)
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
containsKey操作
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
put操作
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
// key為null時(shí)的put操作
if (key == null)
return putForNullKey(value);
// key不為null時(shí)put操作:先查看key是否存在昂拂,存在則更新
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
// 不存在則新加一條entry,modCount++
modCount++;
addEntry(hash, key, value, i);
return null;
}
/**
* Offloaded version of put for null keys
*/
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
/**
* This method is used instead of put by constructors and
* pseudoconstructors (clone, readObject). It does not resize the table,
* check for comodification, etc. It calls createEntry rather than
* addEntry.
*/
private void putForCreate(K key, V value) {
int hash = null == key ? 0 : hash(key);
int i = indexFor(hash, table.length);
/**
* Look for preexisting entry for key. This will never happen for
* clone or deserialize. It will only happen for construction if the
* input Map is a sorted map whose ordering is inconsistent w/ equals.
*/
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) {
e.value = value;
return;
}
}
createEntry(hash, key, value, i);
}
private void putAllForCreate(Map<? extends K, ? extends V> m) {
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
putForCreate(e.getKey(), e.getValue());
}
// put一個(gè)map集合的操作
public void putAll(Map<? extends K, ? extends V> m) {
int numKeysToBeAdded = m.size();
if (numKeysToBeAdded == 0)
return;
if (table == EMPTY_TABLE) {
inflateTable((int) Math.max(numKeysToBeAdded * loadFactor, threshold));
}
/*
* Expand the map if the map if the number of mappings to be added
* is greater than or equal to threshold. This is conservative; the
* obvious condition is (m.size() + size) >= threshold, but this
* condition could result in a map with twice the appropriate capacity,
* if the keys to be added overlap with the keys already in this map.
* By using the conservative calculation, we subject ourself
* to at most one extra resize.
*/
if (numKeysToBeAdded > threshold) {
int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
if (targetCapacity > MAXIMUM_CAPACITY)
targetCapacity = MAXIMUM_CAPACITY;
int newCapacity = table.length;
while (newCapacity < targetCapacity)
newCapacity <<= 1;
if (newCapacity > table.length)
resize(newCapacity);
}
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
put(e.getKey(), e.getValue());
}
resize操作
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
// capacity已經(jīng)最大抛猖,則將threshold設(shè)置為最大值
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
// 構(gòu)造新的數(shù)組
Entry[] newTable = new Entry[newCapacity];
// 將舊數(shù)組中的數(shù)據(jù)轉(zhuǎn)移到新數(shù)組中
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
// 更新threshold
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
/**
* Transfers all entries from current table to newTable.
*/
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
// 遍歷舊數(shù)組當(dāng)中的每一條entry
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
// 在新數(shù)組當(dāng)中的hash值
e.hash = null == e.key ? 0 : hash(e.key);
}
// 重新計(jì)算hash值后在新數(shù)組中的下表
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
hash函數(shù)h & (length-1)詳解
由于length是2的冪格侯,所以length-1每一位都是1,相當(dāng)于取余操作财著,且碰撞的幾率小联四。如果有0,則與的時(shí)候可能不同的數(shù)會(huì)發(fā)生哈希碰撞撑教。同時(shí)朝墩,如果與的時(shí)候有0,那么與的結(jié)果一定是0伟姐,則該位置為1的一些地址始終會(huì)用不到收苏,造成空間浪費(fèi)!
remove操作
public V remove(Object key) {
Entry<K,V> e = removeEntryForKey(key);
return (e == null ? null : e.value);
}
/**
* Removes and returns the entry associated with the specified key
* in the HashMap. Returns null if the HashMap contains no mapping
* for this key.
*/
final Entry<K,V> removeEntryForKey(Object key) {
if (size == 0) {
return null;
}
int hash = (key == null) ? 0 : hash(key);
int i = indexFor(hash, table.length);
// 該索引的第一個(gè)節(jié)點(diǎn)
Entry<K,V> prev = table[i];
Entry<K,V> e = prev;
while (e != null) {
Entry<K,V> next = e.next;
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) {
// 找到要?jiǎng)h除的key
modCount++;
size--;
if (prev == e)
// 如果第一個(gè)就是要?jiǎng)h除的節(jié)點(diǎn)愤兵,直接將下一個(gè)節(jié)點(diǎn)放到table[i]
table[i] = next;
else
// 否則將上一個(gè)節(jié)點(diǎn)的next指向要?jiǎng)h除的下一個(gè)節(jié)點(diǎn)
prev.next = next;
e.recordRemoval(this);
return e;
}
// 沒有找到鹿霸,向后循環(huán)查找
prev = e;
e = next;
}
return e;
}
clear操作
// 將table的每一個(gè)位置置為null,注意并不會(huì)重置capacity
public void clear() {
modCount++;
Arrays.fill(table, null);
size = 0;
}
containsKey/containsValue操作
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
public boolean containsValue(Object value) {
if (value == null)
return containsNullValue();
Entry[] tab = table;
for (int i = 0; i < tab.length ; i++)
for (Entry e = tab[i] ; e != null ; e = e.next)
if (value.equals(e.value))
return true;
return false;
}
private boolean containsNullValue() {
Entry[] tab = table;
for (int i = 0; i < tab.length ; i++)
for (Entry e = tab[i] ; e != null ; e = e.next)
if (e.value == null)
return true;
return false;
}
單項(xiàng)鏈表entry
static class Entry<K,V> implements Map.Entry<K,V> {
// 四個(gè)成員變量秆乳,應(yīng)該牢記
final K key;
V value;
Entry<K,V> next;
int hash;
/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
public final K getKey() {
return key;
}
public final V getValue() {
return value;
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry)o;
Object k1 = getKey();
Object k2 = e.getKey();
if (k1 == k2 || (k1 != null && k1.equals(k2))) {
Object v1 = getValue();
Object v2 = e.getValue();
if (v1 == v2 || (v1 != null && v1.equals(v2)))
return true;
}
return false;
}
public final int hashCode() {
return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue());
}
public final String toString() {
return getKey() + "=" + getValue();
}
/**
* This method is invoked whenever the value in an entry is
* overwritten by an invocation of put(k,v) for a key k that's already
* in the HashMap.
*/
void recordAccess(HashMap<K,V> m) {
}
/**
* This method is invoked whenever the entry is
* removed from the table.
*/
void recordRemoval(HashMap<K,V> m) {
}
}