Consider the follwing HashMap.clear()
code:
/**
* Removes all of the mappings from this map.
* The map will be empty after this call returns.
Looking at the source code, it does look like HashMap
never shrinks. The resize
method is called to double the size whenever required, but doesn't have anything ala ArrayList.trimToSize().
If you're using a HashMap
in such a way that it grows and shrinks dramatically often, you may want to just create a new HashMap
instead of calling clear()
.