Skip to main content

Understanding Equals and hashCode contract,and what can go wrong

SO what about Equals and hashcode?
Most people will say these are the two basic function available in Object class and could be called from any Class object created in Java. Well thats true but there is more then this.
First take a look at HashCode:
Every JVM provides a good hashCode implementation mostly based on the object's memory location and for two different object(by location) it produce different hashCode.
And at the same time Equal method also consider two different memory location object as Not Equal. So at core two are in a contract and works great.

But as soon as start writing code and write a class and create a state of a class(means create few data variables in a class), the core contract of hashCode and Equal doesnt work any more.

Class MyFirstCLass{
int value;
public MyFirstCLass(int value){
this.value = value;

MyFistCLass obj1 = new MyFirstCLass(10);
MyFistCLass obj2 = new MyFirstCLass(10);

Now obj1 and obj2, theoratically should be equal as both holds the same value. Bu the basic implemenataion of equals method in java will say they are not and hashCode will also produce different hashCode value for them(basically confirms that they are not same)

Now i write following code for hash based collection.

Set mySet = new HashSet();

now if i do following
mySet.contains(obj2) //see obj2 and not obj1
it returns false and basically my code doesnt work.

So then we say ok we need to write our own hashCode method, because above code should have worked.
Now Java has said that you should and can implement hashCode and Equal functions but there are few rules you should take care of
    The equals method implements an equivalence relation:
  • It is reflexive: for any reference value x, x.equals(x) should return true.
  • It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.
  • For any non-null reference value x, x.equals(null) should return false.
 and The general contract of hashCode is:
  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
If you want to squeeze out one sentence out of everything i have written then contract between two is
Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes.
(Note:  However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.)

Things to consider when writing equal method
  1. Always check if they are at same memory location by using == operator on two objects
  2. Two different class objects can not be considered equals, so always check if passed object is instance of same class.
  3. Then compare object's property and see if they are equal.
  4. Even if you are 100% sure that your hashCode returns unequal hashCode for unEqual object, still never use it here from performance point of view.
What happens when hashCode and equals break contracts
  1. hashCode return same Value for un equal object. The performance of hashBased collection will go down as there will be more collision.
  2. hashCode return different Value for equal objects. You may not be able to find the exisitng object from hash based collection.