Java 中 HashMap 的工作機制

轉自http://www.oschina.net/question/82993_76564?from=20121104

現在很多的Java程序員都會把HashMap當作一個熱門話題,今天我也來說一說Hashmap。 
我假設你對HashMap感興趣,另外我認爲你已經瞭解了HashMap的基礎,這裏我就不再贅述HashMap是個什麼東東,如果對於你來講HashMap還是一個新概念的話,你可以去看看官方的javadoc. 

在繼續看下去之前,我推薦你看一看我前面的一篇文章: Java 中正確使用 hashCode 和 equals 方法 

目錄: 
1、一句話回答 
2、什麼是哈希 
3、關於Entry類的一點介紹 
4、put()方法實際上做了什麼 
5、get()方法內部工作機制 
6、注意點 

一句話回答 
如果任何人讓我描述一下HashMap的工作機制的話,我就簡單的回答:“基於Hash的規則”。這句話非常簡單,但是要理解這句話之前,首先我們得了解什麼是哈希,不是麼? 

什麼是哈希 
哈希簡單的說就是對變量/對象的屬性應用某種算法後得到的一個唯一的串,用這個串來確定變量/對象的唯一性。一個正確的哈希函數必須遵守這個準則。 

當哈希函數應用在相同的對象或者equal的對象的時候,每次執行都應該返回相同的值。換句話說,兩個相等的對象應該有相同的hashcode。 

注:所有Java對象都從Object類繼承了一個默認的hashCode()方法。這個方法將對象在內存中的地址作爲整數返回,這是一個很好的hash實現,他確保了不同的對象擁有不同的hashcode。 

關於Entry類的一點介紹 
一個map的定義是:一個映射鍵(key)到值(value)的對象。非常簡單對吧。 

所以,在HashMap中一定有一定的機制來存儲這些鍵值對。使得,HashMap有一個內部類Entry,看起來像這樣。 

1 static class Entry<K,V> implementsMap.Entry<K,V>
2 {
3         final K key;
4         V value;
5         Entry<K,V> next;
6         final int hash;
7         ...//More code goes here
8 }

當然,Entry類有屬性用來存儲鍵值對映射。key被final標記,除了key和value,我們還能看到兩個變量next和hash。接下來我們試着理解這些變量的含義。 

put()方法實際上做了什麼 
再進一步看put方法的實現之前,我們有必要看一看Entry實例在數組中的存儲,HashMap中是這樣定義的:
1 /**
2      * The table, resized as necessary. Length MUST Always be a power of two.
3      */
4     transient Entry[] table;
現在再來看put方法的實現。 
01 /**
02 * Associates the specified value with the specified key in this map.
03 * If the map previously contained a mapping for the key, the old
04 * value is replaced.
05 *
06 * @param key key with which the specified value is to be associated
07 * @param value value to be associated with the specified key
08 * @return the previous value associated with <tt>key</tt>, or
09 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>null</tt> if there was no mapping for <tt>key</tt>.
10 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (A <tt>null</tt> return can also indicate that the map
11 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; previously associated <tt>null</tt> with <tt>key</tt>.)
12 */
13 public V put(K key, V value) {
14 if (key == null)
15 return putForNullKey(value);
16 int hash = hash(key.hashCode());
17 int i = indexFor(hash, table.length);
18 for (Entry<K,V> e = table[i]; e != null; e = e.next) {
19 Object k;
20 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
21 V oldValue = e.value;
22 e.value = value;
23 e.recordAccess(this);
24 return oldValue;
25 }
26 }
27  
28 modCount++;
29 addEntry(hash, key, value, i);
30 return null;
31 }
讓我們一步一步的看 
首先,檢查key是否爲null,如果key是null值被存在table[0]的位置,因爲null的hashcode始終爲0 
接下來,通過key的hashCode()方法計算了這個key的hash值,這個hash值被用來計算存儲Entry對象的數組中的位置。JDK的設計者假設會有一些人可能寫出非常差的hashCode()方法,會出現一些非常大或者非常小的hash值。爲了解決這個問題,他們引入了另外一個hash函數,接受對象的hashCode(),並轉換到適合數組的容量大小。 

接着是indexFor(hash,table,length)方法,這個方法計算了entry對象存儲的準確位置。

接下來就是主要的部分,我們都知道兩個不相等的對象可能擁有過相同的hashCode值,兩個不同的對象是怎麼存儲在相同的位置[叫做bucket]呢? 
答案是LinkedList。如果你記得,Entry類有一個next變量,這個變量總是指向鏈中的下一個變量,這完全符合鏈表的特點。 

所以,在發生碰撞的時候,entry對象會被以鏈表的形式存儲起來,當一個Entry對象需要被存儲的時候,hashmap檢查該位置是否已近有了一個entry對象,如果沒有就存在那裏,如果有了就檢查她的next屬性,如果是空,當前的entry對象就作爲已經存儲的entry對象的下一個節點,依次類推。 

如果我們給已經存在的key存入另一個value會怎麼樣的?邏輯上,舊的值將被替換掉。在檢測了Entry對象的存儲位置後,hashmap將會遍歷那個位置的entry鏈表,對每一個entry調用equals方法,這個鏈表中的所有對象都具有相同的hashCode()而equals方法都不等。如果發現equals方法有相等的就執行替換。 

在這種方式下HashMap就能保證key的唯一性。

get方法的工作機制 
現在我們已經瞭解了HashMap中存儲鍵值對的機制。下一個問題是:怎樣從一個HashMap中查詢結果。 
其實邏輯跟put是一樣的,如果傳入的key有匹配就將該位置的value返回,如果沒有就返回null. 

01 /**
02 * Returns the value to which the specified key is mapped,
03 * or {@code null} if this map contains no mapping for the key.
04 *
05 * <p>More formally, if this map contains a mapping from a key
06 * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
07 * key.equals(k))}, then this method returns {@code v}; otherwise
08 * it returns {@code null}.&nbsp; (There can be at most one such mapping.)
09 *
10 * <p>A return value of {@code null} does not <i>necessarily</i>
11 * indicate that the map contains no mapping for the key; it's also
12 * possible that the map explicitly maps the key to {@code null}.
13 * The {@link #containsKey containsKey} operation may be used to
14 * distinguish these two cases.
15 *
16 * @see #put(Object, Object)
17 */
18 public V get(Object key) {
19 if (key == null)
20 return getForNullKey();
21 int hash = hash(key.hashCode());
22 for (Entry<K,V> e = table[indexFor(hash, table.length)];
23 e != null;
24 e = e.next) {
25 Object k;
26 if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
27 return e.value;
28 }
29 return null;
30 }

上面的代碼看起來跟put()方法很像,除了if (e.hash == hash && ((k = e.key) == key || key.equals(k)))。 

注意點 
  • 存儲Entry對象的數據結構是一個叫做Entry類型的table數組。
  • 數組中一個特定的索引位置稱爲bucket,因爲它可以容納一個LinkedList的第一個元素的對象。
  • Key對象的hashCode()需要用來計算Entry對象的存儲位置。
  • Key對象的equals()方法需要用來維持Map中對象的唯一性。
  • get()和put()方法跟Value對象的hashCode和equals方法無關。
  • null的hashCode總是0,這樣的Entry對象總是被存儲在數組的第一個位置

發佈了13 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章