This article describes the need to initialize a HashMap with fitting default values. It is important to know something about the internal implementation of a HashMap to select the best default values for initialization.
is in principal valid Java code, but the hash map is not initialized with a reasonable values.
In this case the Java implementation has to resize the number of buckets with increasing number of data in the hash map. In Java the default load factor is 0.75 and the default initial capacity of a hash map is 16. This means that the threshold for the next resize equates like:
The hash map will automatically rehash the contents of the hash map into a new array with a larger capacity. This happens when the number of data in the hash map exceeds the THRESHOLD. Each resize leads to a doubled capacity 16*0.75, 32*0.75, 64*0.75, etc. The following diagram depicts this:
Setting the initial capacity to just a high value is no solution, because an iteration over collection requires time proportional to the capacity of the hash map. It is important not to set the initial capacity too high if you iterate over the values.
For small hash maps there is no big difference, but for situations with really large hash maps (e.g. 100 MB size) it can seriously increase the needed memory and the rehashing causes a slowdown of the application.