经典Hash实现(采用拉链法处理冲突)(二)
int i = (int)(e.key % newCapacity);
//The order for StorePages with the same key must not change
//that we need to find the end of the link list. This is different to a typical HashTable
if(newTable[i] == null){
newTable[i] = e;
}else{
Entry entry = newTable[i];
while(entry.next != null) entry = entry.next;
entry.next = e;
}
e = next;
} while (e != null);
}
}
}
/**
* Removes the mapping for this key from this map if present.
*
* @param key key whose mapping is to be removed from the map.
* @return previous value associated with specified key, or null
* if there was no mapping for key. A null return can
* also indicate that the map previously associated null
* with the specified key.
*/
final TableStorePage remove(long key) {
int i = (int)(key % table.length);
Entry prev = table[i];
Entry e = prev;
while (e != null) {
Entry next = e.next;
if (e.key == key) {
size--;
if (prev == e)
table[i] = next;
else
prev.next = next;
return e.value;
}
prev = e;
e = next;
}
return null;
}
/**
* Removes all mappings from this map.
*/
final void clear() {
Entry tab[] = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}
/**
* Returns true if this map maps one or more keys to the
* specified value.
*
* @param value value whose presence in this map is to be tested.
* @return true if this map maps one or more keys to the
* specified value.
*/
final boolean containsValue(TableStorePage value) {
Entry tab[] = table;
for (int i = 0; i < tab.length ; i++)
for (Entry e = tab[i] ; e != null ; e = e.next)
if (value.equals(e.value))
return true;
return false;
}
static class Entry{
final long key;
final TableStorePage value;
Entry next;
/**
* Create new entry.
*/
Entry(long k, TableStorePage v, Entry n) {
value = v;
next = n;
key = k;
}
}
}