问题

题接上文

    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();

    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

    public boolean remove(Object o) {
        return map.remove(o)==PRESENT;
    }

会发现为什么调用的HashMap,存的value不用null呢,而是用一个Object

解释

点进HashMapputremove方法,就会发现端倪
map不管是put还是remove都会返回之前的值,不存在返回null
如果使用null作为value的话就会和map该键值对不存在的情况重合,导致无法区分!
代码见文末!

put

    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,  // 流程:1.没有初始化就先初始化 2.判断要插入的槽位有没有值,没有就new, 3. 槽位有值,判断是数组,链表,还是红黑树,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0) // 如果table为null或者长度为0,则进行初始化
            n = (tab = resize()).length;                    // resize()方法本来是用于扩容,由于初始化没有实际分配空间,这里用该方法进行空间分配
        if ((p = tab[i = (n - 1) & hash]) == null)          // 判断指定索引位置是否为空,如果为空,则在该位置插入新的键值对节点。这是 HashMap 在处理插入操作时的关键逻辑,用于实现将键值对映射到正确的位置并进行插入的操作。 - (n - 1) & hash:首先,(n - 1) & hash 是用于计算键的哈希码 hash 在哈希表中的索引位置。n 是哈希表的长度,(n - 1) 的二进制表示会是形如 111...111 的掩码,低位都是 1,高位都是 0。通过与操作 & 将哈希码的低位与掩码进行与运算,可以得到一个范围在 0 到 n-1 的索引值。
            tab[i] = newNode(hash, key, value, null);  // 直接将新的key-value插入到计算的索引i位置
        else {                                              // 如果不为空
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;                                      // 节点key已经有值了 如果节点的key与插入的key相等,则将当前节点赋值给e,表示存在相同的key
            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) // -1 for 1st 达到链表转红黑树大小的阈值8
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))  // 在哈希桶的指定索引位置已经存在了相同的键(key
                        break;
                    p = e; // 这是不断将p往前移,顺着链表遍历
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)   // 如果 onlyIfAbsent 参数为false(替换冲突值) true:不替换 或者 旧值为null
                    e.value = value;                     // 替换值
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

remove

    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 天
  • 访问总数:129054 人次

浙公网安备33011302000604

辽ICP备20003309号