Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How Java Hashmap index is generated via hash value?

I have watched several videos as this one, but could not understand how the index value is generated.

There is a formula as index = hash & (n-1) but it is not mentioned what the "n" is?. So, could you please explain how that index value is generated?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The hash is generated based on the key and you need to compute the corresponding index in the array. That index is limited to the size of the array.

This part: & (n-1) will guarantee that the index value is within 0 <= index <= (n-1) by using a bitwise AND operator.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading