hashmap 原理

總結(jié)一下java 的 HashMap

1.什么是HashMap及HashMap的特點(diǎn)?

鍵值對存儲數(shù)據(jù),最多允許一條數(shù)據(jù)鍵為null,值可以有多個null,非線程安全。

2.HashMap的原理

JDK1.8 中,HashMap是數(shù)組+鏈表+紅黑樹

首先看下部分源碼

transient Node<K,V>[] table;

transient Set<Map.Entry<K,V>> entrySet;

transient int size;

transient int modCount;

int threshold; //所能容納的數(shù)量極限

final float loadFactor;? //負(fù)載因子

static class Node<K,V>?implements Map.Entry<K,V> {

final int hash;

? ? final K key;

? ? V value;

? ? Node<K,V>?next;

? ? Node(int hash, K key, V value, Node<K,V> next) {

this.hash = hash;

? ? ? ? this.key = key;

? ? ? ? this.value = value;

? ? ? ? this.next = next;

? ? }

public final K getKey()? ? ? ? {return key; }

public final V getValue()? ? ? {return value; }

public final String toString() {return key +"=" +value; }

public final int hashCode() {

return Objects.hashCode(key) ^ Objects.hashCode(value);

? ? }

public final V setValue(V newValue) {

V oldValue =value;

? ? ? ? value = newValue;

? ? ? ? return oldValue;

? ? }

public final boolean equals(Object o) {

if (o ==this)

return true;

? ? ? ? if (oinstanceof Map.Entry) {

Map.Entry e = (Map.Entry)o;

? ? ? ? ? ? if (Objects.equals(key, e.getKey()) &&

Objects.equals(value, e.getValue()))

return true;

? ? ? ? }

return false;

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容