前情提要
爲什麼分析jdk7,不直接分析jdk8?
jdk8的源碼做了大幅的改動,已經很複雜了。分析jdk7可以快速理解HashMap設計思想,以及HashMap存在的缺點,知道jdk8爲什麼要做這些改動。
HashMap數據結構
HashMap底層數據結構是數組和單鏈表,對key計算hashCode散列到數組中,相同hashCode的key添加的同一個鏈表中。
HashMap有哪些屬性
// 默認容量16,2的4次方
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
// 默認負載因子,容量閾值超過75%的時候會觸發擴容,
// 也就是超過16*0.0.75=12個。設置太小會頻繁擴容,設置太大影響查詢效率。
static final float DEFAULT_LOAD_FACTOR = 0.75f;
// 容量最大值
static final int MAXIMUM_CAPACITY = 1 << 30;
// 默認空數組,並不是大家想象的容量16,那是在put的時候初始化的
static final Entry<?,?>[] EMPTY_TABLE = {};
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
// 單鏈表的元素節點
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
int hash;
}
如何初始化HashMap
// 無參初始化
public HashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
// 指定容量初始化
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
// initialCapacity默認容量16,loadFactor默認負載因子0.75
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
threshold = initialCapacity;
}
put 方法
public V put(K key, V value) {
// 是空數組,第一次初始化
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
// 如果key是null,就調用put null的方法
if (key == null)
return putForNullKey(value);
// 計算key的hash值
int hash = hash(key);
// 計算key在Entry數組中的位置,相當於對數組長度求餘
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;
return oldValue;
}
}
modCount++;
// 沒找到,就添加
addEntry(hash, key, value, i);
return null;
}
// 第一次初始化時調用
private void inflateTable(int toSize) {
// 找到大於等於size的2的冪次方
int capacity = roundUpToPowerOf2(toSize);
// 計算閾值,16 * 0.75 = 12
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
table = new Entry[capacity];
}
// key是null的put方法
private V putForNullKey(V value) {
// 遍歷下標是0的數組,找到了就覆蓋並返回舊值
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;
}
// hashCode邏輯與(length-1),相當於對(length-1)求餘,
// 所以要保證金數組長度是2的倍數
static int indexFor(int h, int length) {
return h & (length-1);
}
// 添加元素,bucketIndex表示數組下標
void addEntry(int hash, K key, V value, int bucketIndex) {
// 元素個數大於閾值就擴容
if ((size >= threshold) && (null != table[bucketIndex])) {
// 2倍擴容
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
// 擴容後,重新計算數組下標位置
bucketIndex = indexFor(hash, table.length);
}
// 創建元素節點
createEntry(hash, key, value, bucketIndex);
}
// 擴容
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
// 數組最大擴容到2的30次方
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);
}
// 把舊數組的所有元素拷貝到新數組裏
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
// 遍歷數組所有元素
for (Entry<K,V> e : table) {
// 遍歷該下標位置上的鏈表
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
// 重新計算新數組的下標位置
int i = indexFor(e.hash, newCapacity);
// 頭插法
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
// 創建元素,放在頭節點
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}
get 方法
public V get(Object key) {
// key是null,調用單獨方法
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
// key是null,遍歷數組下標是0的鏈表
private V getForNullKey() {
if (size == 0) {
return null;
}
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null)
return e.value;
}
return null;
}
// 獲取Entry數組
final Entry<K,V> getEntry(Object key) {
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;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
看完源碼,我們試一下回答一些常見的面試題。
常見面試題
HashMap的put方法邏輯
判斷entry是否是空數組,如果是初始化一個長度是16,閾值是12的數組
判斷key是否等於null,如果是null,就放在下標是0的數組位置上,並插入頭結點
對key的hashCode二次hash,並對(length-1)邏輯與,算出數組下標位置
遍歷該下標位置上的鏈表,如果找到該key,就覆蓋舊值並返回
判斷當前元素個數是否大於閾值,如果大於就執行2倍擴容,把原數組的元素重新hash到新數組中
用當前key創建一個節點,插到下標數組鏈表的頭結點
爲什麼HashMap的容量必須是2的倍數
static int indexFor(int h, int length) {
return h & (length-1);
}
計算機中,與運算比求餘運算更快,採用了hashCode&(length-1)。
假如length是16,(length-1)的二進制就是 1111,比15小的數邏輯與之後就是自身,比15大的數,只有低4位的數才能運算出值。是爲了更方便與運算。
HashMap線程不安全體現在哪些方面?
由於hash衝突的時候插入鏈表,採用的是頭插法,導致擴容後鏈表的順序和原來順序相反,多個線程同時擴容會出現環形鏈表,get的時候陷入死循環。
讀者福利
我這邊整理了很多家互聯網公司的面試資料(含答案),如下圖
有需要的話可以免費獲取!
獲取方式!關注+點贊+評論666
點擊即可免費領取
(思考)HashMap還有哪些缺點
使用單鏈表解決hash衝突,導致最壞的情況get的效率降至O(N)
鏈表採用頭插法,導致多線程擴容出現環形鏈表
擴容需要把每個元素重新hash放到新數組裏,性能太差
針對這些缺點,你有沒有好的解決辦法,好在這些問題都在jdk8得到解決,下篇一塊手撕jdk8 HashMap源碼。