注:行尾注释为了方便调试jdk1.8源码!

getNode

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 && // 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;                                        // 都没有找到,小hashmap失望极了,呜呜呜~~~
}

removeNode

final Node<K,V> removeNode(int hash, Object key, Object value,      // 注意一点:p这个东西如果是要删除的元素在数组上,那么他就是要删除的那个元素
                           boolean matchValue, boolean movable) {   //          如果是在链表上的话,他永远是要删除元素的前一个元素,因为找到了就break了,没来的及给p更新
    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 &&                        // 如果恰巧这个元素在数组上,没有再链表上,index位置就能直接找到这个家伙的坑位
            ((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;  // hhh ,找到你啦!
                        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;                 // 这里很巧妙:如果tab[index]上是一个链表,就会把他的下一个节点移到数组上!
            else                                        // 在链表上
                p.next = node.next;                     // 则将查找到元素的前一个元素的next指针指向被查找到的元素的next元素
            ++modCount;
            --size;
            afterNodeRemoval(node);
            return node;
        }
    }
    return null;
}

分类: java

站点统计

  • 文章总数:309 篇
  • 分类总数:19 个
  • 标签总数:191 个
  • 运行天数:1009 天
  • 访问总数:129675 人次

浙公网安备33011302000604

辽ICP备20003309号