The hashCode() method of objects is used when you insert them into a HashTable, HashMap or HashSet.

An object’s hash code allows algorithms and data structures to put objects into compartments, just like letter types in a printer’s type case. The printer puts all “A” types into the compartment for “A”, and he looks for an “A” only in this one compartment.

This simple system lets him find types much faster than searching in an unsorted drawer. That’s also the idea of hash-based collections, such as HashMap and HashSet.

In order to make your class work properly with hash-based collections and other algorithms that rely on hash codes, all hashCode implementations must stick to a simple contract.

The hashCode Contract
  1. If two objects are same then they must return same value in hashCode() and equals() method whenever invoked.
  2. It is not necessary that two different object must have different hashcode values. It might be possible that they share common hash bucket. hashCode is used to narrow the search result. When we try to insert any key in HashMap first it checks whether any other object present with same hashcode and if yes then it checks for the equals() method. If two objects are same then HashMap will not add that key instead it will replace the old value by new one.   Note : JVM assigns unique hashcode value to each object when they are created in memory and if developers don’t override the hashcode method then there is no way the two object returns same hashcode value.
  3. Whenever hashCode() method is invoked on the same object more than once within single execution of application, hashCode() must return same integer provided no information or fields used in equals and hashcode is modified. This integer is not required to be same during multiple execution of application though.
hashCode Collisions

Whenever two different objects have the same hash code, we call this a collision.

A collision is nothing critical, it just means that there is more than one object in a single bucket, so a HashMap lookup has to look again to find the right object. A lot of collisions will degrade the performance of a system, but they won’t lead to incorrect results.

But if you mistake the hash code for a unique handle to an object, e.g use it as a key in a Map, then you will sometimes get the wrong object. Because even though collisions are rare, they are inevitable.

For example, the Strings “Aa” and “BB” produce the same hashCode: 2112. Therefore Never misuse hashCode as a key.

Below is the hashCode() method of String Class in Java:

Default value for hash variable is 0. As if condition is true if will convert our string to character array.

Based on criteria h = 31 * h + val[i];, It will multiply old hash with 31 and calculate complete hash for whole string.

An alternative: SHA1

You may know that cryptographic hash codes such as SHA1 are sometimes used to identify objects (Git does this, for example). Is this also unsafe? No. SHA1 uses 160-bit keys, which makes collisions virtually impossible.

Even with a gigantic number of objects, the odds of a collision in this space are far below the odds of a meteor crashing the computer that runs your program.

Leave a Reply

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