美文网首页
HashMap源码解析

HashMap源码解析

作者: herohua | 来源:发表于2020-01-08 16:49 被阅读0次

    HashMap在JDK1.8之前底层的实现方式是数组+链表,从JDK1.8开始对HashMap底层进行了优化,改为由数组+链表+红黑树实现。数组是HashMap的主体,链表和红黑树主要是解决哈希冲突(哈希碰撞)而存在的。

    具体采用数组+链表的方式还是数组+红黑树的方式由节点数决定。

    JDK版本 实现方式 节点数(Entry)>=8 节点数(Entry)<8
    <1.8 数组+链表 数组+单向链表 数组+单向链表
    >=1.8 数组+链表+红黑树 数组+红黑树 数组+单向链表

    1. HashMap的数据结构

    HashMap数据结构.png

    HashMap的最基本的数据结构是数组,根据哈希算法决定放在数组中的索引,如果数组该索引处已经有值,即发生所谓的哈希冲突(哈希碰撞),将该元素封装成Node节点,放在单向链表的尾部,一旦单向链表的长度达到8,则将单向链表转为红黑树。

    Node节点的定义

    static class Node<K,V> implements Map.Entry<K,V> {
          final int hash;     // 对key的hashcode值进行hash运算后得到的值,存储在Entry,避免重复计算
          final K key;
          V value;
          Node<K,V> next;     // 指向下一个节点(Entry)的引用,单链表结构
    
          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;
          }
     }
    

    2. 哈希算法的实现

    static final int hash(Object key) {
            int h;
            return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
        }
    

    最终哈希值的结果分为3步:

    1. 算出key本身的哈希值
    2. 将哈希值右移16位
    3. 对key本身的哈希值与右移16位的哈希值进行异或运算
    哈希算法.png

    得到哈希值,再与(数组的长度-1)进行与(&)运算,就能得到在数组中的索引

    确认数组索引.png

    所以最终确定元素该存储在数组中位置的流程:

    索引确认流程.png

    3. HashMap的put方法

    public V put(K key, V value) {
           return putVal(hash(key), key, value, false, true);
    }
    
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                       boolean evict) {
          Node<K,V>[] tab; Node<K,V> p; int n, i;
    
          // table延迟创建,在put的时候再创建table
          if ((tab = table) == null || (n = tab.length) == 0)
              n = (tab = resize()).length;
    
          // (n - 1) & hash == 在数组中的index值,如果该index还没有节点,则新建一个Node节点(Entry),next为空
          if ((p = tab[i = (n - 1) & hash]) == null)
             tab[i] = newNode(hash, key, value, null);
    
          // 如果该index处有值,即发生哈希冲突
          else {
              Node<K,V> e;
              K k;
              if (p.hash == hash &&
                  ((k = p.key) == key || (key != null && key.equals(k))))
                  // key也相同,将p赋给e,p当前指向数组中index出的节点,即单向链表的头节点或红黑树的根节点
                  e = p;
              else if (p instanceof TreeNode)
                  // key不相同,并且p属于红黑树节点,在红黑树中插入该节点
                  e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
              else {
                  // key 不相同,属于单向链表节点
                  for (int binCount = 0; ;  ++binCount) {
                      // 遍历链表,将该节点放到单向链表末尾
                      if ((e = p.next) == null) {
                          p.next = newNode(hash, key, value, null);
                          // 如果单向链表长度大于等于8,将单向链表转换为红黑树
                          if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                              treeifyBin(tab, hash);
                          break;
                      }
                      // 找到key节点,退出循环
                      if (e.hash == hash &&
                          ((k = e.key) == key || (key != null && key.equals(k))))
                          break;
                      // 没有找到,p指向下一个节点,继续向单向链表后面寻找
                      p = e;
                  }
              }
              // key原本已经存在,判断是否需要更新,onlyIfAbsent=false:更新
              if (e != null) { // existing mapping for key
                  V oldValue = e.value;
                  if (!onlyIfAbsent || oldValue == null)
                      e.value = value;
                  afterNodeAccess(e);
                  return oldValue;    // 返回旧的value
              }
          }
          ++modCount;
          if (++size > threshold)     // threshold = capacity*loadFactory
              resize();   // 调整容量
          afterNodeInsertion(evict);
          return null;
    }
    

    put方法的流程

    put方法流程.png

    4. HashMap的get方法

    public V get(Object key) {
          Node<K,V> e;
          return (e = getNode(hash(key), key)) == null ? null : e.value;
    }
    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) {
              // 判断hash值和key是否相等
              if (first.hash == hash && // always check first node
                  ((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;
    }
    

    5. HashMap的resize扩容方法

    首先了解一下HashMap的相关field的概念

    /**
         * 该表在首次使用时初始化,并根据需要调整大小。长度始终是2的次方。
         */
        transient Node<K,V>[] table;
    
        /**
         * 存储Node节点的集合
         */
        transient Set<Map.Entry<K,V>> entrySet;
    
        /**
         * 实际存储的key-value键值对的个数
         */
        transient int size;
    
        /**
         * HashMap被改变的次数,由于HashMap非线程安全,在对HashMap进行迭代时,
         * 如果期间其他线程的参与导致HashMap的结构发生变化了(比如put,remove等操作),
         * 需要抛出异常ConcurrentModificationException
         */
        transient int modCount;
    
        /**
         * 阈值 = capacity * load factor时,当size达到该值时,需要对HashMap进行扩容
         */
        int threshold;
    
        /**
         * 负载因子,默认位0.75
         */
        final float loadFactor;
    
    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;
              }
              // 老表的容量乘以2,任然小于最大容量,容量和阈值都翻倍
              else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                       oldCap >= DEFAULT_INITIAL_CAPACITY)
                  newThr = oldThr << 1; // double threshold
          }
          else if (oldThr > 0) // initial capacity was placed in threshold
              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; // 更新全局阈值变量
    
            /************下面是将老表的数据复制到扩容后的新表中************/
          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) {  // 老表元素赋给e
                      oldTab[j] = null;   // 老表的元素置为null
                      if (e.next == null)     // 链表只有一个节点
                          newTab[e.hash & (newCap - 1)] = e;  // 重新计算索引,并将e放入新表中
                      else if (e instanceof TreeNode)     // 该索引处时红黑树结构
                          ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                      else { // preserve order
                          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;
     }
    

    相关文章

      网友评论

          本文标题:HashMap源码解析

          本文链接:https://www.haomeiwen.com/subject/ajykactx.html