Java doc says - When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed
In below program -
HashMap<Integer, String> map = new HashMap<Integer, String>();
int i = 1;
while(i<16) {
map.put(i, new Integer(i).toString());
i++;
}
Key is of type Integer, at insertion of 13th to 15th element HashMap capacity remains as 16 and threshold remains same as 12, why ?
Debug screenshot after adding 13th element in map -
args String[0] (id=16)
map HashMap<K,V> (id=19)
entrySet null
hashSeed 0
KeySet null
loadFactor 0.75
modCount 13
size 13
table HashMap$Entry<K,V>[16] (id=25)
threshold 12
values null
i 14
[null, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=11, 12=12, 13=13, null, null]
HashMap with key of type String - HashMap<String, String>
or a custom class - Map<Employee,Integer>
show expected behaviour at 13th insertion
Looks like this behaviour is due to change in internal implementation of HashMap PUT method in recent version of Java 7. After going through source code of multiple versions, found an answer to my question
HashMap put method calls addEntry() to add a new entry -
jdk7-b147 HashMap.addEntry method looks like -
Source code of version 1.7.0_67-b01 looks like -
So, in recent versions of Java, HashMap may not be resized based on threshold alone. If bucket is empty entry would still go in without resizing HashMap
Java 8 may have different behaviour, source code of version 8-b132 shows PUT is completely re implemented -
Java doc may not be updated as frequently as Java versions ! Thanks Stephen