I have several questions about rebuilding HashMaps
when adding new key-values pair. I will ask questions based on these facts (they are true for Oracle JVM, not sure if they are correct for other JVMs):
HashMap
to have a bigger inner table array every time when you grow HashMap bigger than threshold (threshold = loadFactor*numberOfEntries). It does not matter in which bucket the newly created Entry is put - Map will still grow bigger. Even if all of the Entries go into one bucket (i.e. their keys' hashCode()
return the same number).HashMap
does not shrink when data is removed. Even if all keys are removed from HashMap
, the inner size of it's table does not change.Now the questions:
If they are, then:
Yes, these facts are correct.
new HashMap
and let the old one be garbage-collected.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With