Twelve Then Twenty Four
Java's HashMap documentation states that when the number of entries exceeds the product of the load factor and the current capacity, the table is rehashed to roughly twice the number of buckets. It also states that if the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash ever happens. HashMap uses a default capacity of 16, a default load factor of 0.75, and always rounds a requested capacity up to a power of two.
You run:
Map<String, Integer> m = new HashMap<>();
for (int i = 0; i < 40; i++) m.put("key" + i, i);
How many times does the table resize, and what initial capacity passed to the constructor would have avoided every resize?
Sign in to answer questions and track your progress
Sign In