java HashMap內(nèi)部實現(xiàn)原理詳解
詳解HashMap內(nèi)部實現(xiàn)原理
內(nèi)部數(shù)據(jù)結(jié)構(gòu)
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
int hash;
從上面的數(shù)據(jù)結(jié)構(gòu)定義可以看出,HashMap存元素的是一組鍵值對的鏈表,以什么形式存儲呢
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
可以看出,是以數(shù)組形式儲存,好的,現(xiàn)在我們知道,HashMap是以數(shù)組形式存儲,每個數(shù)組里面是一個鍵值對,這個鍵值對還可以鏈接到下個鍵值對。如下圖所示:

hashmap的添加
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
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;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
這里可以看出,hashmap的添加,首先根據(jù)一個entry的hash屬性去查找相應(yīng)的table元素i,然后看這個位置是否有元素存在,如果沒有,直接放入,如果有,遍歷此次鏈表,加到表尾
刪除
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);
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)))) {
modCount++;
size--;
if (prev == e)
table[i] = next;
else
prev.next = next;
e.recordRemoval(this);
return e;
}
prev = e;
e = next;
}
return e;
}
刪除的話,還是先根據(jù)hash在table數(shù)組中查找,然后再根據(jù)equals在鏈表中進行查找,這個也是為什么hashmap和hashset等以hash方式進行存儲的數(shù)據(jù)結(jié)構(gòu)要求實現(xiàn)兩個方法hashcode和equalsd的原因
學(xué)過hash的人都知道,hash表的性能和hash沖突的發(fā)生次數(shù)有很大關(guān)系,但有不能申請過長的table表浪費空間,所以這里有了我們的resize函數(shù)
擴容機制
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
Entry[] newTable = new Entry[newCapacity];
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
這個方法會在put的時候調(diào)用,上面put的時候先調(diào)用 addEntry(hash, key, value, i);方法,然后看addEntry方法
void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
上面可以看出那么 HashMap 當(dāng) HashMap 中的元素個數(shù)超過數(shù)組大小 *loadFactor 時,就會進行數(shù)組擴容,loadFactor 的默認值為 0.75,這是一個折中的取值。也就是說,默認情況下,數(shù)組大小為 16,那么當(dāng) HashMap 中元素個數(shù)超過 16*0.75=12 的時候,就把數(shù)組的大小擴展為 2*16=32,即擴大一倍,然后重新計算每個元素在數(shù)組中的位 置,而這是一個非常消耗性能的操作,所以如果我們已經(jīng)預(yù)知 HashMap 中元素的個數(shù),那么預(yù)設(shè)元素的個數(shù)能夠有效的提高 HashMap 的性能。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
關(guān)于SpringCloud分布式系統(tǒng)中實現(xiàn)冪等性的幾種方式
這篇文章主要介紹了關(guān)于SpringCloud分布式系統(tǒng)中實現(xiàn)冪等性的幾種方式,冪等函數(shù),或冪等方法,是指可以使用相同參數(shù)重復(fù)執(zhí)行,并能獲得相同結(jié)果的函數(shù),這些函數(shù)不會影響系統(tǒng)狀態(tài),也不用擔(dān)心重復(fù)執(zhí)行會對系統(tǒng)造成改變,需要的朋友可以參考下2023-10-10
Java常見的數(shù)據(jù)結(jié)構(gòu)之棧和隊列詳解
這篇文章主要介紹了Java常見的數(shù)據(jù)結(jié)構(gòu)之棧和隊列詳解,棧(Stack) 是一種基本的數(shù)據(jù)結(jié)構(gòu),具有后進先出(LIFO)的特性,類似于現(xiàn)實生活中的一疊盤子,棧用于存儲一組元素,但只允許在棧頂進行插入(入棧)和刪除(出棧)操作,需要的朋友可以參考下2023-10-10
Java自動化實現(xiàn)PowerPoint轉(zhuǎn)換為PDF
在日常的企業(yè)運營和個人工作中,PowerPoint(PPT/PPTX)文件因其強大的演示功能而被廣泛使用,本文將詳細介紹如何利用 Spire.Presentation for Java 庫,通過簡潔高效的代碼實現(xiàn) PowerPoint 到 PDF 的轉(zhuǎn)換,需要的可以了解下2025-08-08

