Files
StudyNote/java/java 集合/JAVA-HashMap文件/getAndput.md
2022-08-25 11:00:54 +08:00

3.9 KiB
Raw Blame History

1.put方法

1.1put方法

// 1.首先计算出key的hash值 然后调用putVal()
    	return putVal(hash(key), key, value, false, true);

1.2putVal方法

HashMap.Node<K,V>[] tab; HashMap.Node<K,V> p; int n, i;
  // 判断table数组是否为空
  if ((tab = table) == null || (n = tab.length) == 0)
    // 为空就扩容
    n = (tab = resize()).length;
  // hash值和n-1与运算 计算出要插入桶的位置
  if ((p = tab[i = (n - 1) & hash]) == null)
    // 如果为空 就直接放置一个node节点将put的数据加入进去
    tab[i] = newNode(hash, key, value, null);
  // 如果不为空则走以下代码(也就是hash冲突)
  else {
    HashMap.Node<K,V> e; K k;
    // 判断当前node的key与要put的key是否相等
    if (p.hash == hash &&
        ((k = p.key) == key || (key != null && key.equals(k))))
      e = p;
    // 判断当前node是否是红黑树的节点
    else if (p instanceof HashMap.TreeNode)
      // 如果是就将要put进去的数据加入到红黑树当中
      e = ((HashMap.TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
    // 如果要put的key与当前node节点的key不相同;也不是红黑树的节点,那么就说明要new一个node加入到链表当		中
    else {
      // 遍历链表(jdk1.8是在链表的尾部插入节点)
      for (int binCount = 0; ; ++binCount) {
        // 判断p.next是否为空并将e作为p.next的引用
        if ((e = p.next) == null) {
          // 如果为空则将要put的数据赋值给p,next也就是e
          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 = e;
      }
    }
    // 如果这里为true,说明在链表当中找到了相同的key或者没有走到链表代码中(头节点的key与要put的key相			同),则是一个更新的操作
    if (e != null) { // existing mapping for key
      V oldValue = e.value;
      if (!onlyIfAbsent || oldValue == null)
        e.value = value;
      afterNodeAccess(e);
      return oldValue;
    }
  }
  // 插入节点
  ++modCount;
  // 若果当前size大于临界值就扩容 (先插入节点在扩容)
  if (++size > threshold)
    resize();
  afterNodeInsertion(evict);
  return null;

2.get方法

2.1get()

Node<K,V> e;
// 如果为空则返回null,不为空则调用getnode()
return (e = getNode(hash(key), key)) == null ? null : e.value;

2.2getNode()

HashMap.Node<K,V>[] tab; HashMap.Node<K,V> first, e; int n; K k;
// 判断此数组是否为空,如果为空返回null,不为空则计算出key的桶位置
if ((tab = table) != null && (n = tab.length) > 0 &&
    (first = tab[(n - 1) & hash]) != null) {
  // 判断头节点是否是要查询的key,如果是直接返回
  if (first.hash == hash && // always check first node
      ((k = first.key) == key || (key != null && key.equals(k))))
    return first;
  // 如果头结点不是,就需要循环遍历链表;判断下一节点是否为空,为空则返回null
  if ((e = first.next) != null) {
    // 判断是否是红黑树,如果是则调用树的查找
    if (first instanceof HashMap.TreeNode)
      return ((HashMap.TreeNode<K,V>)first).getTreeNode(hash, key);
    // 一直循环直到找到相同的key,没有则返回null
    do {
      if (e.hash == hash &&
          ((k = e.key) == key || (key != null && key.equals(k))))
        return e;
    } while ((e = e.next) != null);
  }
}
return null;