美文网首页
java集合源码阅读(二)HashMap

java集合源码阅读(二)HashMap

作者: 坠叶飘香 | 来源:发表于2019-05-28 11:52 被阅读0次

1.总结

  • HashMap非线程安全
  • HashMap的Key和Value可以为null
  • HashMap不保证数据插入顺序

2.resize()方法

/**
 * Initializes or doubles table size.  If null, allocates in
 * accord with initial capacity target held in field threshold.
 * Otherwise, because we are using power-of-two expansion, the
 * elements from each bin must either stay at same index, or move
 * with a power of two offset in the new table.
 *
 * @return the table
 */
final Node<K,V>[] resize() {
  Node<K,V>[] oldTab = table;
  int oldCap = (oldTab == null) ? 0 : oldTab.length; //目前的table的容量
  int oldThr = threshold;
  int newCap, newThr = 0;
  if (oldCap > 0) {//如果之前length大于零
    if (oldCap >= MAXIMUM_CAPACITY) { //之前的length达到最大容量
      threshold = Integer.MAX_VALUE;
      return oldTab; //返回
    }  
    //没超过最大值,就扩充为原来的2倍
    else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY)
      newThr = oldThr << 1; // double threshold
  }
  //如果本来的length小于等于零
  else if (oldThr > 0) // initial capacity was placed in threshold
    newCap = oldThr;
  else {               // zero initial threshold signifies using defaults
    newCap = DEFAULT_INITIAL_CAPACITY; //默认容量:16
    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
  table = newTab;
  if (oldTab != null) {
    for (int j = 0; j < oldCap; ++j) { //遍历老的数组
      Node<K,V> e;
      if ((e = oldTab[j]) != null) {//获取第j个元素
        oldTab[j] = null; //清除老元素
        if (e.next == null) //如果没有链表
          newTab[e.hash & (newCap - 1)] = e; //计算e的hash,存入新table
        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;
}

相关文章

网友评论

      本文标题:java集合源码阅读(二)HashMap

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