總結(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;
? ? }
}