BATJ面試Android高級崗必問;HashMap源碼最全詳解

前言

有大廠工做經驗的Android開發朋友應該都知道,大廠對於java這一塊特別重視,面試必問,並且都是會問到原理源碼層。HashMap就是最常問的一個點java

HashMap是Map族中最爲經常使用的一種,也是 Java Collection Framework 的重要成員。面試

本文首先給出了 HashMap 的實質並概述了其與 Map、HashSet 的關係,緊接着給出了 HashMap 在 JDK 中的定義,並結合源碼分析了其四種構造方式。最後,經過對 HashMap 的數據結構、實現原理、源碼實現三個方面的剖析,深刻到它底層 Hash 存儲機制,解釋了其底層數組長度老是 2 的 n 次方的緣由,也揭示了其快速存取、擴容及擴容後的重哈希的原理與實現。
本文全部關於HashMap的源碼都是基於** JDK 1.6** 的,不一樣 JDK 版本之間也許會有些許差別,但不影響咱們對 HashMap 的數據結構、原理等總體的把握和了解。

HashMap 概述

Map 是 Key-Value 對映射的抽象接口,該映射不包括重複的鍵,即一個鍵對應一個值。HashMap 是 Java Collection Framework 的重要成員,也是Map族(以下圖所示)中咱們最爲經常使用的一種。簡單地說,HashMap 是基於哈希表的 Map 接口的實現,以 Key-Value 的形式存在,即存儲的對象是 Entry (同時包含了 Key 和 Value) 。在HashMap中,其會根據hash算法來計算key-value的存儲位置並進行快速存取。特別地,HashMap最多隻容許一條Entry的鍵爲Null(多條會覆蓋),但容許多條Entry的值爲Null。此外,HashMap 是 Map 的一個非同步的實現。算法


雖然 HashMap 和 HashSet 實現的接口規範不一樣,可是它們底層的 Hash 存儲機制徹底相同。實際上,HashSet 自己就是在 HashMap 的基礎上實現的數組

HashMap 在 JDK 中的定義

HashMap實現了Map接口,並繼承 AbstractMap 抽象類,其中 Map 接口定義了鍵值映射規則。和 AbstractCollection抽象類在 Collection 族的做用相似, AbstractMap 抽象類提供了 Map 接口的骨幹實現,以最大限度地減小實現Map接口所需的工做。HashMap 在JDK中的定義爲:bash

public class HashMap<K, V> extends AbstractMap<K, V>
implements Map<K, V>, Cloneable, Serializable {
    ...
}
複製代碼

HashMap 的構造函數

HashMap 一共提供了四個構造函數,其中 默認無參的構造函數 和 參數爲Map的構造函數 爲 Java Collection Framework 規範的推薦實現,其他兩個構造函數則是 HashMap 專門提供的。數據結構

一、HashMap()app

該構造函數意在構造一個具備> 默認初始容量 (16) 和 默認負載因子(0.75) 的空 HashMap,是 Java Collection Framework 規範推薦提供的,其源碼以下:less

/**
 * Constructs an empty HashMap with the default initial capacity
 * (16) and the default load factor (0.75).
 */
 public HashMap() {
 //負載因子:用於衡量的是一個散列表的空間的使用程度
 this.loadFactor = DEFAULT_LOAD_FACTOR; 
 //HashMap進行擴容的閾值,它的值等於 HashMap 的容量乘以負載因子
 threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
 // HashMap的底層實現還是數組,只是數組的每一項都是一條鏈
 table = new Entry[DEFAULT_INITIAL_CAPACITY];
 init();
 }
複製代碼

二、HashMap(int initialCapacity, float loadFactor)ide

該構造函數意在構造一個 指定初始容量 和 指定負載因子的空 HashMap,其源碼以下:函數

/**
 * Constructs an empty HashMap with the specified initial capacity and load factor.
 */
 public HashMap(int initialCapacity, float loadFactor) {
 //初始容量不能小於 0
 if (initialCapacity < 0)
 throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity);
 //初始容量不能超過 2^30
 if (initialCapacity > MAXIMUM_CAPACITY)
 initialCapacity = MAXIMUM_CAPACITY;
 //負載因子不能小於 0 
 if (loadFactor <= 0 || Float.isNaN(loadFactor))
 throw new IllegalArgumentException("Illegal load factor: " +
 loadFactor);
 // HashMap 的容量必須是2的冪次方,超過 initialCapacity 的最小 2^n 
 int capacity = 1;
 while (capacity < initialCapacity)
 capacity <<= 1; 
 //負載因子
 this.loadFactor = loadFactor;
 //設置HashMap的容量極限,當HashMap的容量達到該極限時就會進行自動擴容操做
 threshold = (int)(capacity * loadFactor);
 // HashMap的底層實現還是數組,只是數組的每一項都是一條鏈
 table = new Entry[capacity];
 init();
 }
複製代碼

三、HashMap(int initialCapacity)

該構造函數意在構造一個指定初始容量和默認負載因子 (0.75)的空 HashMap,其源碼以下:

// Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75)
 public HashMap(int initialCapacity) {
 this(initialCapacity, DEFAULT_LOAD_FACTOR); // 直接調用上述構造函數
 }
複製代碼

四、HashMap(Map<? extends K, ? extends V> m)

該構造函數意在構造一個與指定 Map 具備相同映射的 HashMap,其 初始容量不小於 16 (具體依賴於指定Map的大小),負載因子是 0.75,是 Java Collection Framework 規範推薦提供的,其源碼以下:

// Constructs a new HashMap with the same mappings as the specified Map. 
 // The HashMap is created with default load factor (0.75) and an initial capacity
 // sufficient to hold the mappings in the specified Map.
 public HashMap(Map<? extends K, ? extends V> m) {
 // 初始容量不小於 16 
 this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
 DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
 putAllForCreate(m);
 }
複製代碼

在這裏,咱們提到了兩個很是重要的參數:初始容量 和 負載因子,這兩個參數是影響HashMap性能的重要參數。其中,容量表示哈希表中桶的數量 (table 數組的大小),初始容量是建立哈希表時桶的數量;負載因子是哈希表在其容量自動增長以前能夠達到多滿的一種尺度,它衡量的是一個散列表的空間的使用程度,負載因子越大表示散列表的裝填程度越高,反之愈小

HashMap 的數據結構

哈希的相關概念

Hash 就是把任意長度的輸入(又叫作預映射, pre-image),經過哈希算法,變換成固定長度的輸出(一般是整型),該輸出就是哈希值。這種轉換是一種 壓縮映射 ,也就是說,散列值的空間一般遠小於輸入的空間。不一樣的輸入可能會散列成相同的輸出,從而不可能從散列值來惟一的肯定輸入值。簡單的說,就是一種將任意長度的消息壓縮到某一固定長度的息摘要函數。

哈希的應用:數據結構

咱們知道,數組的特色是:尋址容易,插入和刪除困難而鏈表的特色是:尋址困難,插入和刪除容易。那麼咱們能不能綜合二者的特性,作出一種尋址容易,插入和刪除也容易的數據結構呢?答案是確定的,這就是咱們要提起的哈希表。事實上,哈希表有多種不一樣的實現方法,咱們接下來解釋的是最經典的一種方法 —— 拉鍊法,咱們能夠將其理解爲 鏈表的數組,以下圖所示:


咱們能夠從上圖看到,左邊很明顯是個數組,數組的每一個成員是一個鏈表。該數據結構所容納的全部元素均包含一個指針,用於元素間的連接。咱們根據元素的自身特徵把元素分配到不一樣的鏈表中去,反過來咱們也正是經過這些特徵找到正確的鏈表,再從鏈表中找出正確的元素。其中,根據元素特徵計算元素數組下標的方法就是 哈希算法。

總的來講,哈希表適合用做快速查找、刪除的基本數據結構,一般須要總數據量能夠放入內存。在使用哈希表時,有如下幾個關鍵點:

  • hash 函數(哈希算法)的選擇:針對不一樣的對象(字符串、整數等)具體的哈希方法;
  • 碰撞處理:經常使用的有兩種方式,一種是open hashing,即 >拉鍊法;
  • 另外一種就是 closed hashing,即開地址法(opened addressing)。

HashMap 的數據結構

咱們知道,在Java中最經常使用的兩種結構是 數組 和 鏈表,幾乎全部的數據結構均可以利用這兩種來組合實現,HashMap 就是這種應用的一個典型。實際上,HashMap 就是一個 鏈表數組,以下是它數據結構:



從上圖中,咱們能夠形象地看出HashMap底層實現仍是數組,只是數組的每一項都是一條鏈。其中參數initialCapacity 就表明了該數組的長度,也就是桶的個數。在第三節咱們已經瞭解了HashMap 的默認構造函數的源碼:

/**
 * Constructs an empty HashMap with the default initial capacity
 * (16) and the default load factor (0.75).
 */
 public HashMap() {
 //負載因子:用於衡量的是一個散列表的空間的使用程度
 this.loadFactor = DEFAULT_LOAD_FACTOR; 
 //HashMap進行擴容的閾值,它的值等於 HashMap 的容量乘以負載因子
 threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
 // HashMap的底層實現還是數組,只是數組的每一項都是一條鏈
 table = new Entry[DEFAULT_INITIAL_CAPACITY];
 init();
 }
複製代碼

從上述源碼中咱們能夠看出,每次新建一個HashMap時,都會初始化一個Entry類型的table數組,其中 Entry類型的定義以下:

static class Entry<K,V> implements Map.Entry<K,V> {
 final K key; // 鍵值對的鍵
 V value; // 鍵值對的值
 Entry<K,V> next; // 下一個節點
 final int hash; // hash(key.hashCode())方法的返回值
 /**
 * Creates new entry.
 */
 Entry(int h, K k, V v, Entry<K,V> n) { // Entry 的構造函數
 value = v;
 next = n;
 key = k;
 hash = h;
 }
 ......
}
複製代碼

其中,Entry爲HashMap的內部類,實現了 Map.Entry 接口,其包含了鍵key、值value、下一個節點next,以及hash值四個屬性。事實上,Entry 是構成哈希表的基石,是哈希表所存儲的元素的具體形式。

HashMap 的快速存取

下面咱們結合JDK源碼看HashMap 的存取實現

HashMap 的存儲實現

在 HashMap 中,鍵值對的存儲是經過 put(key,vlaue) 方法來實現的,其源碼以下:

/**
 * Associates the specified value with the specified key in this map.
 * If the map previously contained a mapping for the key, the old
 * value is replaced.
 *
 * @param key key with which the specified value is to be associated
 * @param value value to be associated with the specified key
 * @return the previous value associated with key, or null if there was no mapping for key.
 * Note that a null return can also indicate that the map previously associated null with key.
 */
 public V put(K key, V value) {
 //當key爲null時,調用putForNullKey方法,並將該鍵值對保存到table的第一個位置 
 if (key == null)
 return putForNullKey(value); 
 //根據key的hashCode計算hash值
 int hash = hash(key.hashCode()); // ------- (1)
 //計算該鍵值對在數組中的存儲位置(哪一個桶)
 int i = indexFor(hash, table.length); // ------- (2)
 //在table的第i個桶上進行迭代,尋找 key 保存的位置
 for (Entry<K,V> e = table[i]; e != null; e = e.next) { // ------- (3)
 Object k;
 //判斷該條鏈上是否存在hash值相同且key值相等的映射,若存在,則直接覆蓋 value,並返回舊value
 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
 V oldValue = e.value;
 e.value = value;
 e.recordAccess(this);
 return oldValue; // 返回舊值
 }
 }
 modCount++; //修改次數增長1,快速失敗機制
 //原HashMap中無該映射,將該添加至該鏈的鏈頭
 addEntry(hash, key, value, i); 
 return null;
 }
複製代碼

對NULL鍵的特別處理:putForNullKey()

咱們直接看其源碼:

/**
 * Offloaded version of put for null keys
 */
 private V putForNullKey(V value) {
 // 若key==null,則將其放入table的第一個桶,即 table[0]
 for (Entry<K,V> e = table[0]; e != null; e = e.next) { 
 if (e.key == null) { // 若已經存在key爲null的鍵,則替換其值,並返回舊值
 V oldValue = e.value;
 e.value = value;
 e.recordAccess(this);
 return oldValue;
 }
 }
 modCount++; // 快速失敗
 addEntry(0, null, value, 0); // 不然,將其添加到 table[0] 的桶中
 return null;
 }
複製代碼

HashMap 中的哈希策略(算法)

/**

* Applies a supplemental hash function to a given hashCode, which

* defends against poor quality hash functions. This is critical

* because HashMap uses power-of-two length hash tables, that

* otherwise encounter collisions for hashCodes that do not differ

* in lower bits.

*

* Note: Null keys always map to hash 0, thus index 0.

*/

static int hash(int h) {

// 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);

}
複製代碼

正如JDK官方對該方法的描述那樣,使用hash()方法對一個對象的hashCode進行從新計算是爲了防止質量低下的hashCode()函數實現。因爲hashMap的支撐數組長度老是 2 的冪次,經過右移可使低位的數據儘可能的不一樣,從而使hash值的分佈儘可能均勻。

經過上述hash()方法計算獲得 Key 的 hash值 後,怎麼才能保證元素均勻分佈到table的每一個桶中呢?咱們會想到取模,可是因爲取模的效率較低,HashMap 是經過調用上面的indexFor()方法處理的,其不但簡單並且效率很高,對應源碼以下所示:

/**
 *
 * Returns index for hash code h.
 *
 */
static int indexFor( int h, int length )
{
    return(h & (length - 1) ); /* 做用等價於取模運算,但這種方式效率更高 */
}
複製代碼

HashMap 中鍵值對的添加:addEntry()

咱們直接看其源碼:

/**
 * Adds a new entry with the specified key, value and hash code to
 * the specified bucket. It is the responsibility of this
 * method to resize the table if appropriate.
 *
 * Subclass overrides this to alter the behavior of put method.
 * 
 * 永遠都是在鏈表的表頭添加新元素
 */
 void addEntry(int hash, K key, V value, int bucketIndex) {
 //獲取bucketIndex處的鏈表
 Entry<K,V> e = table[bucketIndex];
 //將新建立的 Entry 鏈入 bucketIndex處的鏈表的表頭 
 table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
 //若HashMap中元素的個數超過極限值 threshold,則容量擴大兩倍
 if (size++ >= threshold)
 resize(2 * table.length);
 }
複製代碼

HashMap 的擴容:resize()

隨着HashMap中元素的數量愈來愈多,發生碰撞的機率將愈來愈大,所產生的子鏈長度就會愈來愈長,這樣勢必會影響HashMap的存取速度。

爲了保證HashMap的效率,系統必需要在某個臨界點進行擴容處理,該臨界點就是HashMap中元素的數量在數值上等於threshold(table數組長度
加載因子)
。可是,不得不說,擴容是一個很是耗時的過程,由於它須要從新計算這些元素在新table數組中的位置並進行復制處理。
因此,若是咱們可以提早預知HashMap 中元素的個數,那麼在構造HashMap時預設元素的個數可以有效的提升HashMap的性能
*。咱們直接看其源碼:

/**
 *
 * Rehashes the contents of this map into a new array with a
 *
 * larger capacity. This method is called automatically when the
 *
 * number of keys in this map reaches its threshold.
 *
 *
 *
 * If current capacity is MAXIMUM_CAPACITY, this method does not
 *
 * resize the map, but sets threshold to Integer.MAX_VALUE.
 *
 * This has the effect of preventing future calls.
 *
 *
 *
 * @param newCapacity the new capacity, MUST be a power of two;
 *
 * must be greater than current capacity unless current
 *
 * capacity is MAXIMUM_CAPACITY (in which case value
 *
 * is irrelevant).
 *
 */
void resize( int newCapacity )
{
    Entry[] oldTable = table;
    int oldCapacity = oldTable.length;
    /* 若 oldCapacity 已達到最大值,直接將 threshold 設爲 Integer.MAX_VALUE */
    if ( oldCapacity == MAXIMUM_CAPACITY )
    {
        threshold = Integer.MAX_VALUE;
        return; /* 直接返回 */
    }
    /* 不然,建立一個更大的數組 */
    Entry[] newTable = new Entry[newCapacity];
    /* 將每條Entry從新哈希到新的數組中 */
    transfer( newTable );
    table = newTable;
    threshold = (int) (newCapacity * loadFactor); /* 從新設定 threshold */
}
複製代碼

HashMap 的重哈希:transfer()

重哈希的主要是一個從新計算原HashMap中的元素在新table數組中的位置並進行復制處理的過程,咱們直接看其源碼:

/**

*

* Transfers all entries from current table to newTable.

*

*/

void transfer( Entry[] newTable )

{

/* 將原數組 table 賦給數組 src */

Entry[] src = table;

int newCapacity = newTable.length;

/* 將數組 src 中的每條鏈從新添加到 newTable 中 */

for ( int j = 0; j < src.length; j++ )

{

Entry<K, V> e = src[j];

if ( e != null )

{

src[j] = null; /* src 回收 */

/* 將每條鏈的每一個元素依次添加到 newTable 中相應的桶中 */

do

{

Entry<K, V> next = e.next;

/* e.hash指的是 hash(key.hashCode())的返回值; */

/* 計算在newTable中的位置,注意原來在同一條子鏈上的元素可能被分配到不一樣的子鏈 */

int i = indexFor( e.hash, newCapacity );

e.next = newTable[i];

newTable[i] = e;

e = next;

}

while ( e != null );

}

}

}
複製代碼

特別須要注意的是,在重哈希的過程當中,原屬於一個桶中的Entry對象可能被分到不一樣的桶,由於HashMap 的容量發生了變化,那麼 h&(length - 1) 的值也會發生相應的變化。極端地說,若是重哈希後,原屬於一個桶中的Entry對象仍屬於同一桶,那麼重哈希也就失去了意義。

HashMap 的讀取實現

相對於HashMap的存儲而言,讀取就顯得比較簡單了。由於,HashMap只需經過key的hash值定位到table數組的某個特定的桶,而後查找並返回該key對應的value便可,源碼以下:

/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
* <p>A return value of {@code null} does not <i>necessarily</i>
* indicate that the map contains no mapping for the key; it's also * possible that the map explicitly maps the key to {@code null}. * The {@link #containsKey containsKey} operation may be used to * distinguish these two cases. * @see #put(Object, Object) */ public V get( Object key ) { /* 若爲null,調用getForNullKey方法返回相對應的value */ if ( key == null ) /* 從table的第一個桶中尋找 key 爲 null 的映射;若不存在,直接返回null */ return(getForNullKey() ); /* 根據該 key 的 hashCode 值計算它的 hash 碼 */ int hash = hash( key.hashCode() ); /* 找出 table 數組中對應的桶 */ for ( Entry<K, V> e = table[indexFor( hash, table.length )]; e != null; e = e.next ) { Object k; /* 若搜索的key與查找的key相同,則返回相對應的value */ if ( e.hash == hash && ( (k = e.key) == key || key.equals( k ) ) ) return(e.value); } return(null); } 複製代碼

針對鍵爲NULL的鍵值對,HashMap 提供了專門的處理:getForNullKey(),其源碼以下:

/**
 *
 * Offloaded version of get() to look up null keys. Null keys map
 *
 * to index 0\. This null case is split out into separate methods
 *
 * for the sake of performance in the two most commonly used
 *
 * operations (get and put), but incorporated with conditionals in
 *
 * others.
 *
 */
private V getForNullKey()
{
    /* 鍵爲NULL的鍵值對若存在,則一定在第一個桶中 */
    for ( Entry<K, V> e = table[0]; e != null; e = e.next )
    {
        if ( e.key == null )
            return(e.value);
    }
    /* 鍵爲NULL的鍵值對若不存在,則直接返回 null */
    return(null);
}
複製代碼

所以,調用HashMap的get(Object key)方法後,若返回值是 NULL,則存在以下兩種可能:

  1. 該 key 對應的值就是 null;
  2. HashMap 中不存在該 key。

本文對於HashMap的源碼介紹到此結束,若是有什麼問題和經驗心得歡迎探討交流。

對源碼理解不是不少的小夥伴也能夠關注我我的介紹,能夠分享HashMap源碼詳解視頻與源碼本身多去琢磨。

相關文章
相關標籤/搜索