HashMap與HashTable原理及數據結構


 

HashMap與HashTable原理及數據結構

hash表結構個人理解

hash表結構,以計算出的hashcode或者在hashcode基礎上加工一個hash值,再通過一個散列算法 獲取到對應的數組地址映射.然后將值存儲到該映射地址上,存儲所在的集合稱為hash表

hash表結構 散列法:元素特征轉變為數組下標的方法。

散列法:元素特征轉變為數組下標的方法 就是個人理解里邊對散列法的概括

網上找的一些散列法:

1,除法散列法 (取余法) 
最直觀的一種,使用的就是這種散列法,公式:  
index = value % 16  
學過匯編的都知道,求模數其實是通過一個除法運算得到的,所以叫“除法散列法”。

2,平方散列法  
求index是非常頻繁的操作,而乘法的運算要比除法來得省時(對現在的CPU來說,估計我們感覺不出來),所以我們考慮把除法換成乘法和一個位移操作。公式:  
h=h >> 12; (無符號右移,除以2^12。記法:左移變大,是乘。右移變小,是除。) 
如果數值分配比較均勻的話這種方法能得到不錯的結果

3, 折疊法:將keyword切割成位數同樣的幾部分,最后一部分位數能夠不同,然后取這幾部分的疊加和(去除進位)作為散列地址 
4, 直接尋址法:取keyword或keyword的某個線性函數值為散列地址。即H(key)=key或H(key) = a•key + b,當中a和b為常數(這樣的散列函數叫做自身函數) 
這幾個算法沒有再細研究,但歸納起來就是讓hash值通過這散列算法獲取更優的映射

HashMap和HashTable的數據結果

這里寫圖片描述
如圖1: 
hash表結構:左側是hash表,右側是單鏈表Entry

HashMap與HashTable相同點

1.二者都是以哈希表數據結構存儲數據. 
2.二者都是以鏈表來作為解決沖突方案:由於不同的對象最終獲取的hash值可能一致,這時候就會在該hash表所對應的鏈表的頭結點插入這個鍵值對. 
3.二者都可以進行數組擴容

HashMap與HashTable異同點

1.是否可以存儲null key,null value不同:HashMap可以存儲null key和null值,HashTable則不允許會報異常,請看(1.1)put調用允許null(1.2)HashTable value為null報錯(1.3)HashTable key調用hash會空指針異常的區別 
HashMap部分源碼:

    public class HashMap{ //(1.1)put的方法調用putForNullKey(k)方法 public V put(K key, V value) { if (table == EMPTY_TABLE) { inflateTable(threshold); } if (key == null) //(1.1)實際key為null時執行的方法 return putForNullKey(value);//可以調用key為null的情況,而且value也沒有限制 int hash = hash(key); int i = indexFor(hash, table.length); //(1.2)如果key一致則替換原先的value 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; e.recordAccess(this); return oldValue; } } modCount++; //(2.1)如果key不一致時執行的方法 addEntry(hash, key, value, i); return null; } //(1.1)可為null時執行的方法 private V putForNullKey(V value) { 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; } //(2.2)hash值不一致時執行的方法 void addEntry(int hash, K key, V value, int bucketIndex) { // (2.3)判斷當前的hash鏈表是否需要擴容 如果超出閾值則進行擴容 擴容為兩倍 if ((size >= threshold) && (null != table[bucketIndex])) { (4.1)擴容 resize(2 * table.length); hash = (null != key) ? hash(key) : 0; bucketIndex = indexFor(hash, table.length); } //(2.4)hash值不一致時執行的方法 createEntry(hash, key, value, bucketIndex); } //(2.5)新增Entry。將“key-value”插入指定位置,bucketIndex是位置索引 void createEntry(int hash, K key, V value, int bucketIndex) {//bucketIndex索引下標 Entry<K,V> e = table[bucketIndex]; //設置“e”為“新Entry的下一個節點” 當索引下標一致的時候新的鍵值對會在鏈表的第一個節點插入 table[bucketIndex] = new Entry<>(hash, key, value, e); size++; } //獲取索引下標 采用&的方式保證獲取的下標小於等於length-1 //取於法的優化 static int indexFor(int h, int length) { return h & (length-1); } //(3.1)提高對象hash碼的質量,重寫hash算法 final int hash(Object k) { int h = hashSeed; if (0 != h && k instanceof String) { return sun.misc.Hashing.stringHash32((String) k); } h ^= k.hashCode(); // 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); } //(4.1)HashMap初始化容器大小16 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 public HashMap() { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); } //(2.2)entry結構 static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; // 指向下一個節點 Entry<K,V> next; final int hash; // 構造函數。 // 輸入參數包括"哈希值(h)", "鍵(k)", "值(v)", "下一節點(n)" Entry(int h, K k, V v, Entry<K,V> n) { value = v; next = n; key = k; hash = h; } public final K getKey() { return key; } } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109

HashTable部分源碼


    public class HashTable{ //(1.2)插入時不允許key或者value為null (2)是線程安全的用synchronized做同步保證 public synchronized V put(K key, V value) { // Make sure the value is not null 上來就報錯 if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. Entry tab[] = table; int hash = hash(key);//這里報錯null調用hashcode方法的時候 (3)取模采用的是hash值去掉負號 再取模長度的方式 int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { V old = e.value; e.value = value; return old; } } modCount++; if (count >= threshold) { // Rehash the table if the threshold is exceeded //(4.1)擴容 rehash(); tab = table; hash = hash(key); index = (hash & 0x7FFFFFFF) % tab.length; } // Creates the new entry. Entry<K,V> e = tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; return null; } //(3.2)hash方法調用 private int hash(Object k) { // hashSeed will be zero if alternative hashing is disabled. return hashSeed ^ k.hashCode(); } //(4.2)默認初始化容器大小 public Hashtable() { this(11, 0.75f); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

2.是否線程安全性不同:HashMap是線程不安全的,HashTable是線程安全的,因為新增或者刪除k,v的時候,比如說新增put方法(參見(2.1)和(2.2)entry結構至(2.5)) 會在鏈表的首個節點插入,如果有多線程進行操作 可能會造成后者覆蓋前者的情況 所以線程不安全.而HashTable則是由synchronized方法做同步保證(所以也導致HashTable的速度較慢). 
3.Hash值計算方式不同:HashMap的獲取索引下標及Hash()方法都不一致,hashmap有對對象的hash碼就行優化,索引的獲取用&計算替代取模計算.(可查看(3.1)和(3.2)) 
4.容器擴容方式不同:HashMap對象的初始化容器是16,而HashTable是11(可看(4)),二者的加載因子默認都是0.75,容器擴容 size(當前已經使用的容器槽位)>=threshold(閥值)=加載因子*initialCapacity(當前容大小)可查看(4.1)和(4.2)擴容; 個人觀點 容器不可設置太小否則需要重新創建HashMap進行值的復制,加載因子默認0.75,設置過小也會需要經常再創建復制,這時候容器存儲的數據還很稀疏.

最后看源碼的過程中有參照了該作者的博客,如您要看源碼請移駕HashMap源碼博客 
地址:http://blog.csdn.net/ns_code/article/details/36034955

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM