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;
}