From java HashMap’s source code it is clear that its space is expanded twice when the space threshold is reached.
I thought about an use case where all 6 elements are stored under same index in linked fashion. The HashMap(size 10) with threshold 7(10*.75) gets expanded when the 7th element arrives. here actually there is no need of expansion since all are saved under one index.
kindly enlighten me
void addEntry(int hash, K key, V value, int bucketIndex)
{
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
void resize(int newCapacity)
{
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
Entry[] newTable = new Entry[newCapacity];
transfer(newTable);
table = newTable;
threshold = (int)(newCapacity * loadFactor);
}
You say there’s no need to resize, since the
HashMapcan hold these entries.However a
HashMapideally should be providing constant access time (O(1)). The resizing occurs in order to try and provide this access time. By reorganising the buckets a lookup for a key should ideally reference a bucket with only one value (to avoid iterating through a list of entries).In the
get()method you’ll find this line:The HashMap is using the
indexFor()method to identify the bucket, and then it will iterate through the buckets to find a matching key. In order to optimise this the iteration should ideally only occur once (you can’t avoid the bucket lookup)This points to hashcodes ideally being equally distributed across the
intrange (2^31-1). You can make an objects hashcode constant (e.g. 1), but then you can see the HashMap can’t do anything but dump all entries in one bucket, and performance is consequently impacted.