HashMap resize

Is there any way to know that HashMap resizing is occurring and how many times?
Chandan PratihastAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chandan PratihastAuthor Commented:
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.