HashMap-Hash冲突解决

用途为个人学习,如有冲突请提醒!

背景:我们常用HashMap作为我们Java开发时的K-V数据存储结构(如id-person,这个ID对应这个人)。我们知道他们的数据结构么,它的Hash值是什么意义。Hash冲突是怎么解决的。我们带着这2个问题将HashMap做个整体剖析。(其实还有一个问题是,它怎么进行动态扩容的)

一、HashMap的数据结构是什么。

下面是HashMap中的源码。其实HashMap的本质是Node数组;K-V结构对应的基础数据结构就是以下源码


  
  
  1. transient HashMap.Node<K, V>[] table;
  2. static class Node<K,V> implements Map.Entry<K,V> {
  3. //Hash值
  4. final int hash;
  5. //Key值
  6. final K key;
  7. //Value值
  8. V value;
  9. //当前Node对应的下个Node(用于解决Hash冲突;稍后讲解)
  10. Node<K,V> next;
  11. Node( int hash, K key, V value, Node<K,V> next) {
  12. this.hash = hash;
  13. this.key = key;
  14. this.value = value;
  15. this.next = next;
  16. }
  17. public final int hashCode() {
  18. return Objects.hashCode(key) ^ Objects.hashCode(value);
  19. }
  20. }

根据上面代码我们知悉,HashMap中基本的数据结构是Node。


  
  
  1. Map persons = new HashMap< String, String>();
  2. //put方法会新建一个Node
  3. //hash=k.hashCode() ^ k >>> 16 ; hash是数组所在位置
  4. //K="1",V="jack";next=null;(无Hash冲突情况)
  5. persons.put( "1", "jack");
  6. persons.put( "2", "john");

上述的Node数据结构中的hash值的意义是将Node均匀的放置在数组[]中。获得hash值后使用在table[(n - 1) & hash] 位置置值。


  
  
  1. /**
  2. * Computes key.hashCode() and spreads (XORs) higher bits of hash
  3. * to lower. Because the table uses power-of-two masking, sets of
  4. * hashes that vary only in bits above the current mask will
  5. * always collide. (Among known examples are sets of Float keys
  6. * holding consecutive whole numbers in small tables.) So we
  7. * apply a transform that spreads the impact of higher bits
  8. * downward. There is a tradeoff between speed, utility, and
  9. * quality of bit-spreading. Because many common sets of hashes
  10. * are already reasonably distributed (so don't benefit from
  11. * spreading), and because we use trees to handle large sets of
  12. * collisions in bins, we just XOR some shifted bits in the
  13. * cheapest possible way to reduce systematic lossage, as well as
  14. * to incorporate impact of the highest bits that would otherwise
  15. * never be used in index calculations because of table bounds.
  16. */
  17. static final int hash(Object var0) {
  18. int var1;
  19. return var0 == null? 0:(var1 = var0.hashCode()) ^ var1 >>> 16;
  20. }

二、HashMap怎么处理hash冲突

上述说道,Node中的hash值是用hash算法得到的,目的是均匀放置,那如果put的过于频繁,会造成不同的key-value计算出了同一个hash,这个时候hash冲突了,那么这2个Node都放置到了数组的同一个位置。HashMap是怎么处理这个问题的呢?

解决方案:HashMap的数据结构是:数组Node[]与链表Node中有next Node.

HashMap中的实际数据结构

(1)如果上述的 persons.put(“1”,”jack”);persons.put(“2”,”john”); 同时计算到的hash值都为123,那么jack先放在第一列的第一个位置Node-jack,persons.put(“2”,”john”);执行时会将Node-jack的next(Node) = Node(john),Jack的下个节点将指向Node(john)。

(2)那么取的时候呢,persons.get(“2”),这个时候取得的hash值是123,即table[123],这时table[123]其实是Node-jack,Key值不相等,取Node-jack的next下个Node,即Node-John,这时Key值相等了,然后返回对应的person.

三、HashMap怎么进行动态扩容

JDK1.7的逻辑相对简单,JDK1.8使用了红黑树TreeMap相对复杂,现在用1.7的进行讲解。本质上其实一样。


  
  
  1. void resize(int newCapacity) { //传入新的容量
  2. Entry[] oldTable = table; //引用扩容前的Entry数组
  3. int oldCapacity = oldTable.length;
  4. if (oldCapacity == MAXIMUM_CAPACITY) { //扩容前的数组大小如果已经达到最大(2^30)了
  5. threshold = Integer.MAX_VALUE; //修改阈值为int的最大值(2^31-1),这样以后就不会扩容了
  6. return;
  7. }
  8. Entry[] newTable = new Entry[newCapacity]; //初始化一个新的Entry数组
  9. transfer(newTable); //!!将数据转移到新的Entry数组里
  10. table = newTable; //HashMap的table属性引用新的Entry数组
  11. threshold = ( int) (newCapacity * loadFactor); //修改阈值
  12. }
  13. void transfer(Entry[] newTable) {
  14. Entry[] src = table; //src引用了旧的Entry数组
  15. int newCapacity = newTable.length;
  16. for ( int j = 0; j < src.length; j++) { //遍历旧的Entry数组
  17. Entry<K, V> e = src[j]; //取得旧Entry数组的每个元素
  18. if (e != null) {
  19. src[j] = null; //释放旧Entry数组的对象引用(for循环后,旧的Entry数组不再引用任何对象)
  20. do {
  21. Entry<K, V> next = e.next;
  22. int i = indexFor(e.hash, newCapacity); //!!重新计算每个元素在数组中的位置
  23. e.next = newTable[i]; //标记[1]
  24. newTable[i] = e; //将元素放在数组上
  25. e = next; //访问下一个Entry链上的元素
  26. } while (e != null);
  27. }
  28. }
  29. }
  30. static int indexFor(int h, int length) {
  31. return h & (length - 1);
  32. }

扩容的方式是新建一个newTab,是oldTab的2倍。遍历oldTab,将oldTab赋值进对应位置的newTab。与ArrayList中的扩容逻辑基本一致,只不过ArrayList是当前容量+(当前容量>>1)。

JDK1.8实现resize()方式


  
  
  1. /*
  2. Initializes or doubles table size. If null, allocates in
  3. * accord with initial capacity target held in field threshold.
  4. * Otherwise, because we are using power-of-two expansion, the
  5. * elements from each bin must either stay at same index, or move
  6. * with a power of two offset in the new table.
  7. @return the table
  8. /
  9. final Node<K,V>[] resize() {
  10. Node<K,V>[] oldTab = table;
  11. int oldCap = (oldTab == null) ? 0 : oldTab.length;
  12. int oldThr = threshold;
  13. int newCap, newThr = 0;
  14. if (oldCap > 0) {
  15. if (oldCap >= MAXIMUM_CAPACITY) {
  16. threshold = Integer.MAX_VALUE;
  17. return oldTab;
  18. }
  19. else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
  20. oldCap >= DEFAULT_INITIAL_CAPACITY)
  21. newThr = oldThr << 1; // double threshold
  22. }
  23. else if (oldThr > 0) // initial capacity was placed in threshold
  24. newCap = oldThr;
  25. else { // zero initial threshold signifies using defaults
  26. newCap = DEFAULT_INITIAL_CAPACITY;
  27. newThr = ( int)(DEFAULT_LOAD_FACTOR DEFAULT_INITIAL_CAPACITY);
  28. }
  29. if (newThr == 0) {
  30. float ft = ( float)newCap * loadFactor;
  31. newThr = (newCap < MAXIMUM_CAPACITY && ft < ( float)MAXIMUM_CAPACITY ?
  32. ( int)ft : Integer.MAX_VALUE);
  33. }
  34. threshold = newThr;
  35. @SuppressWarnings({ "rawtypes", "unchecked"})
  36. Node<K,V>[] newTab = (Node<K,V>[]) new Node[newCap];
  37. table = newTab;
  38. if (oldTab != null) {
  39. for ( int j = 0; j < oldCap; ++j) {
  40. Node<K,V> e;
  41. if ((e = oldTab[j]) != null) {
  42. oldTab[j] = null;
  43. if (e.next == null)
  44. newTab[e.hash & (newCap - 1)] = e;
  45. else if (e instanceof TreeNode)
  46. ((TreeNode<K,V>)e).split( this, newTab, j, oldCap);
  47. else { // preserve order
  48. Node<K,V> loHead = null, loTail = null;
  49. Node<K,V> hiHead = null, hiTail = null;
  50. Node<K,V> next;
  51. do {
  52. next = e.next;
  53. if ((e.hash & oldCap) == 0) {
  54. if (loTail == null)
  55. loHead = e;
  56. else
  57. loTail.next = e;
  58. loTail = e;
  59. }
  60. else {
  61. if (hiTail == null)
  62. hiHead = e;
  63. else
  64. hiTail.next = e;
  65. hiTail = e;
  66. }
  67. } while ((e = next) != null);
  68. if (loTail != null) {
  69. loTail.next = null;
  70. newTab[j] = loHead;
  71. }
  72. if (hiTail != null) {
  73. hiTail.next = null;
  74. newTab[j + oldCap] = hiHead;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. return newTab;
  81. }




猜你喜欢

转载自blog.csdn.net/lfw0735/article/details/81189534