hashcode() – Performance impact on HashMap

As you might now, important part of HashMap is hash function which determines which bucket that key-value pair will go to. This makes hashcode() method in key object very important. Just to put it in perspective, below is a small experiment.

Same hashcode all the time !

If a key object coded in a way to send same hashcode all the time, then eventually all the key-value pairs will end up in same bucket inside HashMap. If your HashMap has n key-value pairs, then while you try to get the value back, HashMap will have to iterate through all n key-value pairs.

Lets take example of map of employee & department. Below Employee class has equals overridden but hashcode() method return 1 all the time.

This is Department object just to use as value

Now lets test the performance by loading 10,000 key-value pairs in a map. After loading we will try to get all the values back & record how much time it takes.

Output:

 

It took around 2547 mills (might slightly vary every time) to get all the elements due to iteration in same bucket.

Different hashcodes

Now lets try same example by implementing hashcode  i.e. lets use hashcode() of member variable i.e. “return firstName.hashCode();”

 

Now when we do the same test above, here is the latest output

 

Time got down to 6 ms which is huge reduction. So basically hashcode plays vital role in HashMap performance. But still this doesn’t mean you have to go to extreme lengths to make hashcode unique. You can follow code as advised by author “Joshua Bloch” or you can try builder provided by Apache i.e. HashCodeBuilder . There might also be several other alternatives present online on different blogs or sites which can be followed.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *