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.


Popular posts from this blog

How to add custom user attributes in keycloak and access them in spring boot application

Sometime it may be possible you want to add more parameters to standard registration page of keyloak for your users and aaccess that data in your spring boot application. This artical will show step by steps on how to add such extra attributes. What is Keycloak Keycloak is an open source software product to allow single sign-on with Identity Management and Access Management aimed at modern applications and services, to learn more visit  h ttps:// What is Spring boot Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". To learn more visit To add an extra attribute in keyclkoak server you will need to edit actuall html template and then registaer new attribute in json response so that it will be available on client. Edit HTML template Lets assume we want to add mobile number on default registration page. Go to Keycloak home installation directory edit fil

How to run Bash commands in parallel and wait before all commands finished

In this article i will explain how you can run commands in parallel and make sure you wait until all commands and have finished and then move forward. Say you have a script which downloads few files or yu have to compile different maven project(independent of each other) or something else which can run in parallel and you have got a good machine which can run these tasks in parallel so that overall you can run the full task in lowest time. For this you can use back ground task and wait from unix. When you add & to any command that command runs in background What is wait command? wait is a built-in command of Linux that waits for completing any running process. wait command is used with a particular process id or job id. When multiple processes are running in the shell then only the process id of the last command will be known by the current shell. If wait command is executed this time, then it will be applied for the last command. If no process id or job id is given with wai

How to create Spring boot cloud config server

What is Spring boot cloud config server? Spring Cloud Config Server provides an HTTP resource-based API for external configuration (name-value pairs or equivalent YAML content). The server is embeddable in a Spring Boot application, by using the  @EnableConfigServer  annotation. Consequently, the following application is a config server In Simple terms, Spring boot cloud config server takes your application.yml or from your spring boot application and serve it over HTTP, and spring application which need to use it just need to delete the application.yml or and create one bootstrap.yml/properties and defined 2 simple properties  and Create New java project using one of following instructions. Create project with Java, maven and Intellij Edit POM.xml, add following content in pom.xml First specify the packaging jar also add parent of this pom to be NOTE : You can lookup the latest version