调试JDK源码-一步一步看HashMap怎么Hash和扩容
調(diào)試JDK源碼-一步一步看HashMap怎么Hash和擴(kuò)容
調(diào)試JDK源碼-ConcurrentHashMap實(shí)現(xiàn)原理
調(diào)試JDK源碼-HashSet實(shí)現(xiàn)原理
調(diào)試JDK源碼-調(diào)試JDK源碼-Hashtable實(shí)現(xiàn)原理以及線程安全的原因
?
?
?
還是調(diào)試源碼最好。
開發(fā)環(huán)境? JDK1.8+NetBeans8.1
說(shuō)明:調(diào)試HashMap的 public V put(K key, V value) 方法并查看key的值時(shí)不能顯示變量的值,原因在于oracle提供的jre中rt.jar不帶debug信息。
orcale在編譯src時(shí)使用了 javac -g:none,意思是不帶任何調(diào)試信息,這樣可以減小rt.jar的大小。若想正常調(diào)試jdk,就只能重新編譯src.zip。
當(dāng)然也可以只編譯單個(gè)需要關(guān)注的java即可,例如HashMap.java。
?
一.解壓src.zip
解壓src.zip到E:\workspace\下,
src.zip在安裝的C:\Program Files\Java\jdk1.8.0_25下
?
二.javac -g重編譯
重新編譯src\java\util下的HashMap.java
Windows下進(jìn)入DOS環(huán)境,輸入
E:\workspace\src\java\util
然后再輸入E:就直接到了E:\workspace\src\java\util
默認(rèn)如果不帶-g編譯是沒(méi)有調(diào)試信息是不夠的。
# javac -g HashMap.java
?
三.替換rt.jar
將編譯好的所有的HashMap.class都放入C:\Program Files\Java\jdk1.8.0_25\jre\lib的rt.jar
說(shuō)明:需要做好備份以防搞錯(cuò)。
參考:eclipse如何debug調(diào)試jdk源碼
初調(diào)HashMap,如何修改JDK的源碼進(jìn)行調(diào)試
編譯JDK源代碼,開啟Debug信息
?
四.調(diào)試HashMap
先看看HashMap的理論吧
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;public class TestHash {@Testpublic void testHashMap() throws Exception {System.out.println("==========================");Map<String, String> m = new HashMap<String, String>();for (int i = 0; i < 18; i++) {m.put((char) (i + 65) + (char) (i + 66) + (char) (i + 67) + "", i + ">>>http://blog.csdn.net/unix21/");}System.out.println("==========================");}
}
?
?下面是源碼
/*** 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 <tt>key</tt>, or* <tt>null</tt> if there was no mapping for <tt>key</tt>.* (A <tt>null</tt> return can also indicate that the map* previously associated <tt>null</tt> with <tt>key</tt>.)*/public V put(K key, V value) {return putVal(hash(key), key, value, false, true);}/*** Implements Map.put and related methods** @param hash hash for key* @param key the key* @param value the value to put* @param onlyIfAbsent if true, don't change existing value* @param evict if false, the table is in creation mode.* @return previous value, or null if none*/final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {Node<K,V>[] tab; Node<K,V> p; int n, i;if ((tab = table) == null || (n = tab.length) == 0)n = (tab = resize()).length;if ((p = tab[i = (n - 1) & hash]) == null)tab[i] = newNode(hash, key, value, null);else {Node<K,V> e; K k;if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))e = p;else if (p instanceof TreeNode)e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);else {for (int binCount = 0; ; ++binCount) {if ((e = p.next) == null) {p.next = newNode(hash, key, value, null);if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}if (e != null) { // existing mapping for keyV oldValue = e.value;if (!onlyIfAbsent || oldValue == null)e.value = value;afterNodeAccess(e);return oldValue;}}++modCount;if (++size > threshold)resize();afterNodeInsertion(evict);return null;}
?
?1.第一次進(jìn)入源碼
先初始化增長(zhǎng)因子
?
一開始聲明一個(gè)
transient Node<K,V>[] table;
java 的transient關(guān)鍵字為我們提供了便利,你只需要實(shí)現(xiàn)Serilizable接口,將不需要序列化的屬性前添加關(guān)鍵字transient,序列化對(duì)象的時(shí)候,這個(gè)屬性就不會(huì)序列化到指定的目的地中。
Java transient關(guān)鍵字使用小記
?
函數(shù)體內(nèi)聲明一個(gè)Node<K,V>[] tab
一開始table=null,所以tab也是null的
可以看到n=16,如果不使用-g編譯是看不到n的,這說(shuō)明初始的tab長(zhǎng)度是16。
然后給tab進(jìn)行初始化,p=tab[0]=null
?
2.插入newNode
最終會(huì)調(diào)用static class Node<K,V>的Node(int hash, K key, V value, Node<K,V> next)
/*** Basic hash bin node, used for most entries. (See below for* TreeNode subclass, and in LinkedHashMap for its Entry subclass.)*/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 (o instanceof Map.Entry) {Map.Entry<?,?> e = (Map.Entry<?,?>)o;if (Objects.equals(key, e.getKey()) &&Objects.equals(value, e.getValue()))return true;}return false;}}
第一個(gè)Node節(jié)點(diǎn)就有值了,其next為null.
?
關(guān)于靜態(tài)嵌套類
?
?
?
3.回到putVal
tab[0]就是返回的Node
?
4.查看是否需要擴(kuò)容
還不到threshold的上限12?,所以無(wú)需擴(kuò)容。
?
5.HashMap第二次put進(jìn)入putVal
很顯然這個(gè)時(shí)候table不為空,因?yàn)榍按我呀?jīng)插值了。
?
i=3,p=tab[3]
新的node插入在tab[3]上,此次依然無(wú)需擴(kuò)容。
?
第4次插值
?
第7次插值
?
第11次
?
第12次
?
第13次
?
tab和
?
此次需要擴(kuò)容
?
點(diǎn)開oldTab
下一步
下一步
?
下一步,threshold升為24
下一步
?
newTab
?
oldTab
?
?oldTab[0]
?
oldTab[j] = null;
?
下一步
?
下一步next = e.next;
?
下一步
?
下一步
下一步
下一步(e = next) != null
下一步
?
經(jīng)過(guò)N此循環(huán)之后
?
newTab
?
oldTab
?
回到putVal
?
擴(kuò)容之后再次進(jìn)入第14次進(jìn)入
?
tab
?
?
關(guān)于HashMap就分析到此,網(wǎng)上有幾篇寫的不錯(cuò)的帖子結(jié)合看看就更明白了,建議閱讀下:
深入Java集合學(xué)習(xí)系列:HashMap的實(shí)現(xiàn)原理?引文 ?深入Java集合學(xué)習(xí)系列:HashMap的實(shí)現(xiàn)原理?原文
HashMap什么時(shí)候進(jìn)行擴(kuò)容呢?當(dāng)HashMap中的元素個(gè)數(shù)超過(guò)數(shù)組大小*loadFactor時(shí),就會(huì)進(jìn)行數(shù)組擴(kuò)容,loadFactor的默認(rèn)值為0.75,這是一個(gè)折中的取值。
也就是說(shuō),默認(rèn)情況下,數(shù)組大小為16,那么當(dāng)HashMap中元素個(gè)數(shù)超過(guò)16*0.75=12(這個(gè)值就是代碼中的threshold值,也叫做臨界值)的時(shí)候,就把數(shù)組的大小擴(kuò)展為 2*16=32,即擴(kuò)大一倍,
然后重新計(jì)算每個(gè)元素在數(shù)組中的位置,而這是一個(gè)非常消耗性能的操作,所以如果我們已經(jīng)預(yù)知HashMap中元素的個(gè)數(shù),那么預(yù)設(shè)元素的個(gè)數(shù)能夠有效的提高HashMap的性能。
?
總結(jié)
以上是生活随笔為你收集整理的调试JDK源码-一步一步看HashMap怎么Hash和扩容的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机科学精彩帖子收集
- 下一篇: Spring源码分析【4】-Spring