Java HashMap implementation has ‘next’ member in Entry private class. Since, a new value for a key will override the old value, what’s the use of ‘next’ member in the Entry class.
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;
/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
.....
}
nextrefers to the next entry in the same bucket.You can have multiple entries in each bucket — a bucket contains all the entries with hash code equal to some
imod 2^n for somen, not just the entry for one particular key.