NutzCN Logo
问答 这个remove方法会不会删除节点?看代码
发布于 3159天前 作者 qq_B961929188CCD397673F9505827728CB 2099 次浏览 复制 上一个帖子 下一个帖子
标签:

V remove(Object key, int hash, Object value) {
lock();
try {
int c = count - 1;
HashEntry<K, V>[] tab = table;
int index = hash & (tab.length - 1);
HashEntry<K, V> first = tab[index];
HashEntry<K, V> e = first;
while (e != null && (e.hash != hash || !key.equals(e.key)))
e = e.next;

                            V oldValue = null;
                            if (e != null) {
                                    V v = e.value;
                                    if (value == null || value.equals(v)) {
                                            oldValue = v;
                                            // All entries following removed node can stay
                                            // in list, but all preceding ones need to be
                                            // cloned.
                                            ++modCount;
                                            HashEntry<K, V> newFirst = e.next;
                                            for (HashEntry<K, V> p = first; p != e; p = p.next) {
                                                    newFirst = new HashEntry<K, V>(p.key, p.hash,
                                                                    newFirst, p.value);
                                                    newFirst.linkNext = p.linkNext;
                                                    newFirst.linkPrev = p.linkPrev;
                                            }
                                            tab[index] = newFirst;
                                            count = c; // write-volatile
                                            // 移除节点
                                            removeNode(e);
                                    }
                            }
                            return oldValue;
                    } finally {
                            unlock();
                    }
            }


         void removeNode(HashEntry<K, V> entry) {
                    entry.linkPrev.linkNext = entry.linkNext;
                    entry.linkNext.linkPrev = entry.linkPrev;
            }
1 回复

刚看了仿hashMap的代码,发现remove只是把链表中的一节点取消关联,却没有对他进行删除,怎么回事?

添加回复
请先登陆
回到顶部