java.util.HashMap源码一直是面试必问的项目,包括底层数据结构、扩容机制,以及为什么线程不安全等。本文作为JDK源码分析系列的开端,首先从HashMap开始,对其源码进行剖析。源码基于JDK1.8
关于红黑树的部分,本文由于篇幅有限,不再展开。
在介绍具体方法前,有必要说明下hashMap的数据结构。
hashMap 内部的最小单元是java.util.HashMap.Node,Node部分源码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 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;     } } 
Node用于存储每个键值对,每个Node除了key、value外,还保存有自身key的hash值,以及指向下一个Node的引用next。
在JDK1.8中,hashMap 本质是由 Node 组成的 数组+链表+红黑树 的混合体。
因此,hashMap 中的方法,其实都是对这个混合体所做的增删改查。
Node数组,hashMap 的主体
1 transient  Node<K,V>[] table;
所有键值对的引用,并没有真的存放键值对
1 transient  Set<Map.Entry<K,V>> entrySet;
所有Node的数量,包括数组、链表和红黑树中的Node,与Node数组长度没有关系
记录hashMap 被修改的次数
扩容阈值,当Node数量达到这个值,则触发扩容
负载系数,决定了扩容阈值的大小,loadFactor = threshold / table.length
默认数组长度,16
1 static  final  int  DEFAULT_INITIAL_CAPACITY  =  1  << 4 ;
最大数组长度,1 << 30 = 1073741824
1 static  final  int  MAXIMUM_CAPACITY  =  1  << 30 ;
默认负载系数,0.75
1 static  final  float  DEFAULT_LOAD_FACTOR  =  0.75f ;
链表转红黑树的长度,不带头结点是8
1 static  final  int  TREEIFY_THRESHOLD  =  8 ;
红黑树转链表的长度,不带头结点是6
1 static  final  int  UNTREEIFY_THRESHOLD  =  6 ;
链表转红黑树要求的最小数组长度是64,当小于这个值会优先扩容
1 static  final  int  MIN_TREEIFY_CAPACITY  =  64 ;
hashMap构造方法有四种,按是否初始化table,可以分为两类
第一类在创建对象阶段只是定义initialCapacity或loadFactor的值,并不初始化table数组。而要等到第一次put操作时才初始化。是一种延迟加载的思想。
无参构造,设定负载系数为默认值
1 2 3 public  HashMap ()  {    this .loadFactor = DEFAULT_LOAD_FACTOR; } 
可设定初始化table数组的长度,同时设定负载系数为默认值。内部调用构造三
1 2 3 4 public  HashMap (int  initialCapacity)  {         this (initialCapacity, DEFAULT_LOAD_FACTOR); } 
可设定初始化table数组的长度以及负载系数。需要注意initialCapacity不是实际的数组长度
1 2 3 4 5 6 7 8 9 10 11 public  HashMap (int  initialCapacity, float  loadFactor)  {    if  (initialCapacity < 0 )         throw  new  IllegalArgumentException ("Illegal initial capacity: "  + initialCapacity);     if  (initialCapacity > MAXIMUM_CAPACITY)         initialCapacity = MAXIMUM_CAPACITY;     if  (loadFactor <= 0  || Float.isNaN(loadFactor))         throw  new  IllegalArgumentException ("Illegal load factor: "  + loadFactor);     this .loadFactor = loadFactor;          this .threshold = tableSizeFor(initialCapacity); } 
重点介绍下tableSizeFor方法,该方法作用是,返回大于给定参数的最小的二次幂。
例如cap=3,返回4;cap=9,返回16;cap=25,返回32。这样不论传入的值是多少,都能保证数组的长度是二次幂。
1 2 3 4 5 6 7 8 9 static  final  int  tableSizeFor (int  cap)  {    int  n  =  cap - 1 ;     n |= n >>> 1 ;     n |= n >>> 2 ;     n |= n >>> 4 ;     n |= n >>> 8 ;     n |= n >>> 16 ;     return  (n < 0 ) ? 1  : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1 ; } 
关于方法实现原理,参考HashMap的tableSizeFor()方法原理 
将Map转换成HashMap,会在添加元素之前先初始化table数组
首先设定负载系数为默认值
1 2 3 4 public  HashMap (Map<? extends K, ? extends V> m)  {    this .loadFactor = DEFAULT_LOAD_FACTOR;     putMapEntries(m, false ); } 
然后调用putMapEntries方法,在putVal方法内初始化table并将map中的元素添加到hashMap中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 final  void  putMapEntries (Map<? extends K, ? extends V> m, boolean  evict)  {    int  s  =  m.size();     if  (s > 0 ) {         if  (table == null ) {                          float  ft  =  ((float )s / loadFactor) + 1.0F ;             int  t  =  ((ft < (float )MAXIMUM_CAPACITY) ?                      (int )ft : MAXIMUM_CAPACITY);             if  (t > threshold)                                  threshold = tableSizeFor(t);         }         else  if  (s > threshold)             resize();         for  (Map.Entry<? extends  K , ? extends  V > e : m.entrySet()) {             K  key  =  e.getKey();             V  value  =  e.getValue();                          putVal(hash(key), key, value, false , evict);         }     } } 
put方法整体逻辑如下:
先根据key的hashCode计算hash值,再根据hash值与数组最大索引的与运算,计算节点在数组中的位置;
 
如果该位置为空,则直接加入节点;
 
如果该位置有节点,则判断key是否相同,相同则替换节点value;
 
如果key不同,则继续判断链表的下一个节点key是否相同;
 
直到遍历到尾节点,将当前节点放在链表末尾;
 
如果链表长度(包括头节点)>=9且数组长度 < 64,则执行扩容;
 
如果链表长度(包括头节点)>=9且数组长度 >= 64,则将当前链表转为红黑树;
 
如果节点数 > 扩容阈值,则扩容。
 
 
1 2 3 public  V put (K key, V value)  {    return  putVal(hash(key), key, value, false , true ); } 
这里不是直接使用的hashCode。h是一个32位的数,h >>> 16 表示h的高16位,h ^ (h >>> 16)表示h的低16位与高16位进行异或运算,作为新的低16位,原有的高16位在异或运算后保持不变。目的在于使结果更加离散。
1 2 3 4 static  final  int  hash (Object key)  {    int  h;     return  (key == null ) ? 0  : (h = key.hashCode()) ^ (h >>> 16 ); } 
调用putVal方法,将键值对添加到数组中。
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 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 )                         treeifyBin(tab, hash);                     break ;                 }                                  if  (e.hash == hash &&                     ((k = e.key) == key || (key != null  && key.equals(k))))                     break ;                                  p = e;             }         }                  if  (e != null ) {                          V  oldValue  =  e.value;             if  (!onlyIfAbsent || oldValue == null )                                  e.value = value;                          afterNodeAccess(e);             return  oldValue;         }     }          ++modCount;          if  (++size > threshold)                  resize();          afterNodeInsertion(evict);     return  null ; } 
重点看一下这一句,将数组长度-1和hash值进行与运算
1 p = tab[i = (n - 1 ) & hash] 
由于n始终是2的m次方,所以n-1的二进制一定是111…111(m个)。而与运算的规则是都为1则是1,所以(n - 1) & hash的结果其实取决于hash值的低m位(从右往左数),所以(n - 1) & hash的结果在000…000(m个)到111…111(m个)之间。也就是说i的值在0到n-1之间。
相比hashTable中用hash值对数组长度取余的做法,虽然也能获得在数组中的位置,但位运算显然更高效。
1 int  index  =  (hash & 0x7FFFFFFF ) % tab.length;
在putVal方法中可以看到,当链表长度(包含头结点)>=9时 ,会进入treeifyBin方法
代码中判断条件是if(binCount >= 7),
第一次进入循环时,节点数是2(p和e),对应binCount =0;
最后一次进入循环时,节点数是9(到p是8个,再加上newNode),对应binCount =7.
treeifyBin方法转红黑树前会先判断数组长度是否达到64。
如果数组长度 < 64,优先扩容(避免过早引入红黑树)。
如果数组长度 >= 64,则执行链表转红黑树逻辑。
具体逻辑是:先将普通节点Node转换成红黑树节点TreeNode,然后补充prev属性,将单向链表转换成双向链表,最后通过TreeNode#treeify方法将双向列表转换成红黑树。
源码如下:
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 final  void  treeifyBin (Node<K,V>[] tab, int  hash)  {    int  n, index; Node<K,V> e;          if  (tab == null  || (n = tab.length) < MIN_TREEIFY_CAPACITY)         resize();     else  if  ((e = tab[index = (n - 1 ) & hash]) != null ) {                  TreeNode<K,V> hd = null , tl = null ;         do  {                           TreeNode<K,V> p = replacementTreeNode(e, null );             if  (tl == null )                                  hd = p;             else  {                                  p.prev = tl;                 tl.next = p;             }                          tl = p;         } while  ((e = e.next) != null );                  if  ((tab[index] = hd) != null )                          hd.treeify(tab);     } } 
当HashMap中数组未初始化,或节点数超出阈值,或链表长度达到8而数组长度不到64时,会触发扩容。
每次扩容,新数组长度总是旧数组的两倍,且数组长度为2的幂次方。因此在扩容后,节点在数组中的位置只有两种可能:保持不变或移动一个旧数组长度。
相对应的,HashMap提供了扩容方法,却没有提供缩容方法,即数组长度只能增大不能减小。
扩容的逻辑如下:
初始化新数组,其长度是旧数组的两倍 
从旧数组索引为0开始,遍历数组节点
节点为空,则移动到数组下一位 
只有一个节点,则重新计算节点的索引,并放置到新数组上 
不止一个节点,则判断节点类型
如果是树节点,则调用树节点的拆分方法,将红黑树拆分成两棵(或一棵)。如果树中节点数<=6,则转换成链表。最后放到新数组对应的位置上 
如果是链表节点,则重新计算每个节点的索引,拆分成两条链表(或一条)。最后放到新数组对应的位置上 
 
 
 
 
 
由于扩容是按照链表的方向进行,因此链表的顺序在扩容后依然得到保证,即在同一条链表(或红黑树)中,先插入节点始终在后插入节点的前面
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 110 111 112 113 114 final  Node<K,V>[] resize() {         Node<K,V>[] oldTab = table;          int  oldCap  =  (oldTab == null ) ? 0  : oldTab.length;          int  oldThr  =  threshold;          int  newCap, newThr = 0 ;          if  (oldCap > 0 ) {                  if  (oldCap >= MAXIMUM_CAPACITY) {                          threshold = Integer.MAX_VALUE;             return  oldTab;         }                  else  if  ((newCap = oldCap << 1 ) < MAXIMUM_CAPACITY &&                  oldCap >= DEFAULT_INITIAL_CAPACITY)             newThr = oldThr << 1 ;     }          else  if  (oldThr > 0 )                  newCap = oldThr;     else  {                   newCap = DEFAULT_INITIAL_CAPACITY;         newThr = (int )(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);     }          if  (newThr == 0 ) {                  float  ft  =  (float )newCap * loadFactor;         newThr = (newCap < MAXIMUM_CAPACITY && ft < (float )MAXIMUM_CAPACITY ?                   (int )ft : Integer.MAX_VALUE);     }          threshold = newThr;          @SuppressWarnings({"rawtypes","unchecked"})           Node<K,V>[] newTab = (Node<K,V>[])new  Node [newCap];          table = newTab;               if  (oldTab != null ) {                  for  (int  j  =  0 ; j < oldCap; ++j) {             Node<K,V> e;             if  ((e = oldTab[j]) != null ) {                                  oldTab[j] = null ;                 if  (e.next == null )                                          newTab[e.hash & (newCap - 1 )] = e;                 else  if  (e instanceof  TreeNode)                                          ((TreeNode<K,V>)e).split(this , newTab, j, oldCap);                 else  {                                           Node<K,V> loHead = null , loTail = null ;                                          Node<K,V> hiHead = null , hiTail = null ;                     Node<K,V> next;                     do  {                                                  next = e.next;                                                  if  ((e.hash & oldCap) == 0 ) {                                                          if  (loTail == null )                                                                  loHead = e;                             else                                                                   loTail.next = e;                                                          loTail = e;                         } else  {                                                           if  (hiTail == null )                                                                  hiHead = e;                             else                                                                   hiTail.next = e;                                                          hiTail = e;                         }                                              } while  ((e = next) != null );                                          if  (loTail != null ) {                                                  loTail.next = null ;                                                  newTab[j] = loHead;                     }                                          if  (hiTail != null ) {                                                  hiTail.next = null ;                                                  newTab[j + oldCap] = hiHead;                     }                 }             }         }     }     return  newTab; } 
重点:
在链表只有一个节点时,通过下面这句确定新e在新数组中的位置
1 newTab[e.hash & (newCap - 1 )] = e; 
这一句非常巧妙,newCap - 1的二进制是111…111(n+1个),oldCap - 1的二进制是111…111(n个),前者比后者多一个1位。所以e.hash & (newCap - 1)和e.hash & (oldCap - 1)的差别关键就在hash值的最高位上。最高位为0则结果相同,最高位为1则二者相差一个最高位的1,而这个1表示的大小正好就是2^n=oldCap。
所以扩容的结果就是:这个节点要么位置不变,要么移动oldCap长度 
 
在判断链表上节点是否移动时,用了这一句
因为oldCap的二进制是1000…000,所以上面这句实际就是判断hash的最高为是否为0,由上面分析可知,为0就表示不移动。
 
 
根据hash值定位到在数组中的位置,如果节点是在数组上,则让该位置为null。如果节点在链表或红黑树上,则移除节点的引用即可。
1 2 3 4 5 public  V remove (Object key)  {    Node<K,V> e;     return  (e = removeNode(hash(key), key, null , false , true )) == null  ?         null  : e.value; } 
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 final  Node<K,V> removeNode (int  hash, Object key, Object value,                            boolean  matchValue, boolean  movable)  {    Node<K,V>[] tab; Node<K,V> p; int  n, index;     if  ((tab = table) != null  && (n = tab.length) > 0  &&                  (p = tab[index = (n - 1 ) & hash]) != null ) {         Node<K,V> node = null , e; K k; V v;                  if  (p.hash == hash &&             ((k = p.key) == key || (key != null  && key.equals(k))))                          node = p;                  else  if  ((e = p.next) != null ) {                          if  (p instanceof  TreeNode)                                  node = ((TreeNode<K,V>)p).getTreeNode(hash, key);             else  {                  do  {                                          if  (e.hash == hash &&                         ((k = e.key) == key ||                          (key != null  && key.equals(k)))) {                                                  node = e;                         break ;                     }                                          p = e;                 } while  ((e = e.next) != null );             }         }         if  (node != null  && (!matchValue || (v = node.value) == value ||                              (value != null  && value.equals(v)))) {             if  (node instanceof  TreeNode)                                  ((TreeNode<K,V>)node).removeTreeNode(this , tab, movable);             else  if  (node == p)                                  tab[index] = node.next;             else                                   p.next = node.next;                          ++modCount;                          --size;                          afterNodeRemoval(node);             return  node;         }     }     return  null ; } 
先根据key的hash值定位到在数组中的位置,再遍历链表或红黑树,key相同则返回value
1 2 3 4 public  V get (Object key)  {    Node<K,V> e;     return  (e = getNode(hash(key), key)) == null  ? null  : e.value; } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 final  Node<K,V> getNode (int  hash, Object key)  {    Node<K,V>[] tab; Node<K,V> first, e; int  n; K k;          if  ((tab = table) != null  && (n = tab.length) > 0  && (first = tab[(n - 1 ) & hash]) != null ) {                  if  (first.hash == hash && ((k = first.key) == key || (key != null  && key.equals(k))))             return  first;                  if  ((e = first.next) != null ) {             if  (first instanceof  TreeNode)                                  return  ((TreeNode<K,V>)first).getTreeNode(hash, key);             do  {                                  if  (e.hash == hash && ((k = e.key) == key || (key != null  && key.equals(k))))                     return  e;                            } while  ((e = e.next) != null );         }     }     return  null ; } 
一直想当然认为keySet方法会返回一个java.util.Set集合,里面包含了所有节点的key。实际上keySet方法并没有真的返回一个保存key的集合,而是提供了一个对节点的引用,真正的遍历操作还是通过table数组来完成的。下面跟随源码,看看内部是如何实现的。
keySet 是HashMap从java.util.AbstractMap继承而来的成员变量,在初次调用keySet方法时,keySet 为空。因此会创建一个java.util.HashMap.KeySet对象。
1 2 3 4 5 6 7 8 9 10 11 public  Set<K> keySet ()  {         Set<K> ks = keySet;     if  (ks == null ) {                  ks = new  KeySet ();                  keySet = ks;     }     return  ks; } 
我们知道,在使用增强for循环遍历set集合时,底层实际调用的是迭代器。
查看KeySet源码,发现重写了iterator方法,内部返回一个java.util.HashMap.KeyIterator对象。
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 final  class  KeySet  extends  AbstractSet <K> {    public  final  int  size ()                  { return  size; }     public  final  void  clear ()                { HashMap.this .clear(); }               public  final  Iterator<K> iterator ()      { return  new  KeyIterator (); }     public  final  boolean  contains (Object o)  { return  containsKey(o); }     public  final  boolean  remove (Object key)  {         return  removeNode(hash(key), key, null , false , true ) != null ;     }     public  final  Spliterator<K> spliterator ()  {         return  new  KeySpliterator <>(HashMap.this , 0 , -1 , 0 , 0 );     }               public  final  void  forEach (Consumer<? super  K> action)  {         Node<K,V>[] tab;         if  (action == null )             throw  new  NullPointerException ();         if  (size > 0  && (tab = table) != null ) {             int  mc  =  modCount;             for  (int  i  =  0 ; i < tab.length; ++i) {                 for  (Node<K,V> e = tab[i]; e != null ; e = e.next)                     action.accept(e.key);             }             if  (modCount != mc)                 throw  new  ConcurrentModificationException ();         }     } } 
KeyIterator继承了java.util.HashMap.HashIterator。在next方法内部调用了父类的nextNode方法,并返回key
1 2 3 4 5 final  class  KeyIterator  extends  HashIterator     implements  Iterator <K> {          public  final  K next ()  { return  nextNode().key; } } 
调用KeyIterator的构造方法前,会先隐式调用HashIterator的构造方法。该构造方法会初始化next,并使其指向第一个不为null的数组节点。
nextNode方法的作用是:每调用一次,返回next节点,并使next在所在链表上移动一次。直到链表上数据为空,则移动到下一个不为null的数组节点。
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 abstract  class  HashIterator  {    Node<K,V> next;             Node<K,V> current;          int  expectedModCount;       int  index;                     HashIterator() {                  expectedModCount = modCount;                  Node<K,V>[] t = table;         current = next = null ;         index = 0 ;         if  (t != null  && size > 0 ) {                           do  {} while  (index < t.length && (next = t[index++]) == null );         }     }         public  final  boolean  hasNext ()  {         return  next != null ;     }          final  Node<K,V> nextNode ()  {         Node<K,V>[] t;                  Node<K,V> e = next;                          if  (modCount != expectedModCount)             throw  new  ConcurrentModificationException ();                if  (e == null )             throw  new  NoSuchElementException ();                         if  ((next = (current = e).next) == null  && (t = table) != null ) {             do  {} while  (index < t.length && (next = t[index++]) == null );         }         return  e;     }   } 
由此可以看见,keySet方法返回的并不是一个真正的“集合”,那么集合常见的功能必然也是受限的。
HashMap.KeySet完整源码如下:
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 final  class  KeySet  extends  AbstractSet <K> {    public  final  int  size ()                  { return  size; }     public  final  void  clear ()                { HashMap.this .clear(); }     public  final  Iterator<K> iterator ()      { return  new  KeyIterator (); }     public  final  boolean  contains (Object o)  { return  containsKey(o); }     public  final  boolean  remove (Object key)  {         return  removeNode(hash(key), key, null , false , true ) != null ;     }     public  final  Spliterator<K> spliterator ()  {         return  new  KeySpliterator <>(HashMap.this , 0 , -1 , 0 , 0 );     }     public  final  void  forEach (Consumer<? super  K> action)  {         Node<K,V>[] tab;         if  (action == null )             throw  new  NullPointerException ();         if  (size > 0  && (tab = table) != null ) {             int  mc  =  modCount;             for  (int  i  =  0 ; i < tab.length; ++i) {                 for  (Node<K,V> e = tab[i]; e != null ; e = e.next)                     action.accept(e.key);             }             if  (modCount != mc)                 throw  new  ConcurrentModificationException ();         }     } } 
支持remove方法,内部也是调用了HashMap#removeNode方法,与HashMap#remove实现原理相同。
由于未重写add方法,所以在执行时会调用到父类java.util.AbstractCollection#add方法,会报UnsupportedOperationException异常。从另一方面考虑,Map中的基本单元是键值对,add方法只添加一个key肯定是不对的。
1 2 3 4 public  boolean  add (E e)  {    throw  new  UnsupportedOperationException (); } 
说完keySet方法,values方法原理相似。
values 是HashMap从java.util.AbstractMap继承而来的成员变量。首次调用values方法时,values 为空,因此会创建一个java.util.HashMap.Values对象
1 2 3 4 5 6 7 8 public  Collection<V> values ()  {    Collection<V> vs = values;     if  (vs == null ) {         vs = new  Values ();         values = vs;     }     return  vs; } 
Values类重写了iterator方法,返回一个java.util.HashMap.ValueIterator迭代器
1 2 3 4 5 6 final  class  Values  extends  AbstractCollection <V> {    public  final  Iterator<V> iterator ()  {        return  new  ValueIterator ();      } } 
ValueIterator类重写了next方法,内部同样调用了HashIterator#nextNode方法,区别是这里取了value值
1 2 3 4 final  class  ValueIterator  extends  HashIterator     implements  Iterator <V> {     public  final  V next ()  { return  nextNode().value; } } 
entrySet方法也相似。区别在于entrySet 是HashMap的成员变量,初次调用entrySet方法会返回一个java.util.HashMap.EntrySet对象
1 2 3 4 public  Set<Map.Entry<K,V>> entrySet() {    Set<Map.Entry<K,V>> es;     return  (es = entrySet) == null  ? (entrySet = new  EntrySet ()) : es; } 
EntrySet类内部重写了iterator方法,返回一个java.util.HashMap.EntryIterator对象
1 2 3 4 5 final  class  EntrySet  extends  AbstractSet <Map.Entry<K,V>> {    public  final  Iterator<Map.Entry<K,V>> iterator() {         return  new  EntryIterator ();     } } 
EntryIterator类重写了next方法,内部同样调用了HashIterator#nextNode方法
1 2 3 4 final  class  EntryIterator  extends  HashIterator     implements  Iterator <Map.Entry<K,V>> {     public  final  Map.Entry<K,V> next ()  { return  nextNode(); } } 
computeIfAbsent方法是JDK1.8中新加的方法,该方法接收一个key和一个函数式接口,仅当key对应节点不存在或key对应value为null时 才生效。
具体逻辑如下:
computeIfAbsent方法源码如下:
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 public  V computeIfAbsent (K key, Function<? super  K, ? extends V> mappingFunction)  {    if  (mappingFunction == null )         throw  new  NullPointerException ();     int  hash  =  hash(key);     Node<K,V>[] tab; Node<K,V> first; int  n, i;     int  binCount  =  0 ;     TreeNode<K,V> t = null ;          Node<K,V> old = null ;          if  (size > threshold || (tab = table) == null  || (n = tab.length) == 0 )         n = (tab = resize()).length;          if  ((first = tab[i = (n - 1 ) & hash]) != null ) {                  if  (first instanceof  TreeNode)             old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);         else  {             Node<K,V> e = first; K k;                          do  {                 if  (e.hash == hash &&                     ((k = e.key) == key || (key != null  && key.equals(k)))) {                     old = e;                     break ;                 }                 ++binCount;             } while  ((e = e.next) != null );         }         V oldValue;                  if  (old != null  && (oldValue = old.value) != null ) {             afterNodeAccess(old);             return  oldValue;         }     }               V  v  =  mappingFunction.apply(key);          if  (v == null ) {         return  null ;     } else  if  (old != null ) {                  old.value = v;         afterNodeAccess(old);         return  v;     }     else  if  (t != null )                  t.putTreeVal(this , tab, hash, key, v);     else  {                  tab[i] = newNode(hash, key, v, first);                  if  (binCount >= TREEIFY_THRESHOLD - 1 )             treeifyBin(tab, hash);     }     ++modCount;     ++size;     afterNodeInsertion(true );     return  v; } 
computeIfPresent方法接收一个key和一个函数式接口,仅当key对应节点存在且key对应value不为null时 才生效。
具体逻辑如下:
computeIfPresent方法源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public  V computeIfPresent (K key, BiFunction<? super  K, ? super  V, ? extends V> remappingFunction)  {    if  (remappingFunction == null )         throw  new  NullPointerException ();     Node<K,V> e; V oldValue;     int  hash  =  hash(key);          if  ((e = getNode(hash, key)) != null  && (oldValue = e.value) != null ) {                  V  v  =  remappingFunction.apply(key, oldValue);                  if  (v != null ) {             e.value = v;             afterNodeAccess(e);             return  v;         }         else                           removeNode(hash, key, null , false , true );     }     return  null ; } 
compute方法接收一个key和一个函数式接口,与上面两者相比,无论key对应节点是否存在 ,都生效
具体逻辑如下:
key对应节点存在
且函数式接口有结果时,用该结果替换value 
且函数式接口结果为null,删除节点 
 
 
key对应节点不存在,且函数式接口有结果
且对应桶是树节点时,将key和该函数式接口的值作为新节点,添加到红黑树中 
且对应桶是链表节点时,将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 public  V compute (K key, BiFunction<? super  K, ? super  V, ? extends V> remappingFunction)  {    if  (remappingFunction == null )         throw  new  NullPointerException ();     int  hash  =  hash(key);     Node<K,V>[] tab; Node<K,V> first; int  n, i;     int  binCount  =  0 ;     TreeNode<K,V> t = null ;          Node<K,V> old = null ;     if  (size > threshold || (tab = table) == null  || (n = tab.length) == 0 )         n = (tab = resize()).length;     if  ((first = tab[i = (n - 1 ) & hash]) != null ) {         if  (first instanceof  TreeNode)             old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);         else  {             Node<K,V> e = first; K k;             do  {                 if  (e.hash == hash &&                     ((k = e.key) == key || (key != null  && key.equals(k)))) {                     old = e;                     break ;                 }                 ++binCount;             } while  ((e = e.next) != null );         }     }     V  oldValue  =  (old == null ) ? null  : old.value;          V  v  =  remappingFunction.apply(key, oldValue);          if  (old != null ) {                  if  (v != null ) {             old.value = v;             afterNodeAccess(old);         }         else                           removeNode(hash, key, null , false , true );     }          else  if  (v != null ) {                  if  (t != null )             t.putTreeVal(this , tab, hash, key, v);         else  {                          tab[i] = newNode(hash, key, v, first);                          if  (binCount >= TREEIFY_THRESHOLD - 1 )                 treeifyBin(tab, hash);         }         ++modCount;         ++size;         afterNodeInsertion(true );     }     return  v; } 
merge方法接收一个key,一个value和一个函数式接口为参数,与compute方法相比,可以指定一个默认的value
具体逻辑如下:
key对应节点存在
且对应value不为null,则将函数式接口的值替换旧对应value 
且对应value为null,则将value替换对应value 
 
 
key对应节点不存在,且value不为null
且对应桶是树节点时,将key和value作为新节点,添加到红黑树中 
且对应桶是链表节点时,将key和value作为新节点,添加到链表中 
 
 
 
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 public  V merge (K key, V value,                BiFunction<? super  V, ? super  V, ? extends V> remappingFunction)  {    if  (value == null )         throw  new  NullPointerException ();     if  (remappingFunction == null )         throw  new  NullPointerException ();     int  hash  =  hash(key);     Node<K,V>[] tab; Node<K,V> first; int  n, i;     int  binCount  =  0 ;     TreeNode<K,V> t = null ;          Node<K,V> old = null ;     if  (size > threshold || (tab = table) == null  ||         (n = tab.length) == 0 )         n = (tab = resize()).length;     if  ((first = tab[i = (n - 1 ) & hash]) != null ) {         if  (first instanceof  TreeNode)             old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);         else  {             Node<K,V> e = first; K k;             do  {                 if  (e.hash == hash &&                     ((k = e.key) == key || (key != null  && key.equals(k)))) {                     old = e;                     break ;                 }                 ++binCount;             } while  ((e = e.next) != null );         }     }          if  (old != null ) {         V v;                  if  (old.value != null )             v = remappingFunction.apply(old.value, value);         else                           v = value;                  if  (v != null ) {             old.value = v;             afterNodeAccess(old);         } else                           removeNode(hash, key, null , false , true );         return  v;     }          if  (value != null ) {         if  (t != null )                          t.putTreeVal(this , tab, hash, key, value);         else  {                          tab[i] = newNode(hash, key, value, first);             if  (binCount >= TREEIFY_THRESHOLD - 1 )                                  treeifyBin(tab, hash);         }         ++modCount;         ++size;         afterNodeInsertion(true );     }     return  value; } 
HashMap的tableSizeFor()方法原理