HashMap resize

Chandan Pratihast
Chandan Pratihast used Ask the Experts™
on
Is there any way to know that HashMap resizing is occurring and how many times?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
There is no way to directly query when this occurs, however, it is reasonably simple enough to calculate, and this should given you a reasonable idea of when and how many resizing (rehashin) operations occur.

Simply... if you add a new entry and the size > (capacity * load factor) then a rehash will occur, and the new capacity will double.

For example, if you use the default HashMap constructor and have a Map with capacity = 16 and load factor = 0.75, and then you add 30 unique entries, you will get one rehash at around the 13th added entry (size 13 is greater than capacity 16 * load factor 0.75), the new capacity will be around 32. Then you will get another rehash around the 25th added entry (size 25 is greater than capacity 32 * load factor 0.75), making a grand total of 2 rehashes for this particular scenario.

Hope this helps

Author

Commented:
Thanks.
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
You're welcome!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial