equals() and hashCode() methods of Object Class


HashTable, HashMap and HashSet are the Collection classes in java.util package that make use of hashing algorithm to store objects. In all these Collection classes except HashSet, objects are stored as key-value pairs. For the storage and the retrieval of any user-defined objects it is a good practice to override the following methods which is mentioned below,

  • hashCode()
  • equals()

These methods are available in the Object class and hence available to all java classes.Using these two methods, an object can be stored or retrieved from a Hashtable, HashMap or HashSet.

hashCode() method

This method returns a hashcode value as an int for the object. Default implementation for hashcode() should be overridden in order to make searching of data faster. The implementation of hashCode() method for an user-defined object should be calculated based on the properties of the class which we wish to consider.

equals() method

This method returns a boolean which specifies whether two objects are equal or not. The default implementation of equals() method given by the Object Class uses the ‘==’ operator to compare two object references, and returns true only if they refer to the same object. But, we can meaningfully re-define this equals() method to have en equality check based on our own criterias.

Consider the following code, which defines two user defined classes Employee and EmployeeId which are supposed to be stored in a Map.

Employee.java

 

public class Employee {

private String name;

public Employee(String name){

this.name = name;

}

public String toString(){

return name;

}

}

EmployeeId.java

 

public class EmployeeId {

private String id;

public EmployeeId(String id){

this.id = id;

}

public String toString(){

return id;

}

}

The following class makes use of the above classes by storing it in a Map for later retrieval. We are adding Employee objects into the Map keyed with Employee Id.

HashCodeTest.java

 

public class HashCodeTest {

public static void main(String[] args) {

Map<EmployeeId, Employee> employees = new HashMap<EmployeeId, Employee>();

employees.put(new EmployeeId(“111”), new Employee(“Johny”));

employees.put(new EmployeeId(“222”), new Employee(“Jeny”)); // Line A

employees.put(new EmployeeId(“333”), new Employee(“Jessie”));

Employee emp =  employees.get(new EmployeeId(“222”)); // Line B

System.out.println(emp); // Line C

}

}

In Line B, we try to retrieve the Employee object who has Employee Id with a value of 222. We expect the output to be ‘Jeny’, because the Employee with Employee Id (222) was already there in the Collection, but surprisingly, the output of the above code is null. The reason is that we did not override the equals() method for EmployeeId and Employee classes because the default implementation of equals() in the Object class considers the new EmployeeId(“222”) in the put statement and new EmployeeId(“222”) in the get statement as two different instances, and hence the call to get() in Line B returns null.

Let us look at how the same code works when we provide our desired implementation for hashcode() and equals() methods. We basically override hashcode() here just to make the object to be searched fast.

Employee.java

 

public class Employee {

private String name;

public Employee(String name){

this.name = name;

}

public String toString(){

return name;

}

 

@Override

public boolean equals(Object obj){

if(obj == null) {

return false;

}

if(obj.getClass() != getClass()){

return false;

}

Employee emp = (Employee)obj;

if(this.name == emp.name){

return true;

}

return false;

}

@Override

public int hashCode(){

return name.hashCode();

}

}

EmployeeId.java

 

public class EmployeeId {

private String id;

public EmployeeId(String id){

this.id = id;

}

public String toString(){

return id;

}

 

public boolean equals(Object obj){

if(obj == null)

return false;

if(obj.getClass() != getClass()){

return false;

}

 

EmployeeId empId = (EmployeeId)obj;

if(this.id == empId.id){

return true;

}

return false;

}

@Override

public int hashCode(){

return id.hashCode();

}

}

Now, we get the desired output ‘Jeny’, because as per our implementation for the equals() method, the new EmployeeId(“222”) in the put statement and new EmployeeId(“222”) in the get statement are considered one and the same.

 

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , , ,
Posted in JAVA
One comment on “equals() and hashCode() methods of Object Class
  1. […] equals() and hashCode() methods of Object Class […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 680 other followers

Calendar
January 2012
M T W T F S S
« Jul   Feb »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 222,930 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100
All tags
Apache Cassandra Arrays in Java Cassandra Cassandra Database Cassandra examples client stubs creating a java web service creating a web service using java difference between interface and abstract class Differences between SOAP and RESTfull webservices Display div as Overlay using JQuery download the spring basic application File directory Generating webservice client stubs Hibernate hibernate exceptions How to display over using JQuery how to generated web service client stubs in STS How to implement Spring basic application how to write singleton design pattern in java interface in java Java Java Arrays Java Example Programs Java examples Java interview questions java programs java project Java sample Programs JavaScript Java Script JavaServer Faces Java Sorting alogorithms Java Tutorials javatutorials Java Web services JDBC open shift openshift Overlay implementation using JQuery Overlay in JQuery POI API POI Examples public class public static void main Redhat OpenShift representational state transfer Restfull webservices Sample Overlay html with JQuery sample programs in java Service-oriented architecture Singleton design pattern in java SOAP SOAP Webservices software Spring Framework Spring Interview Questions Spring MVC Spring MVC Example program Spring MVC sample Program Spring Sample Programs Spring Tutorials Struts technology Top Java Interview questions w3c dom Web service web service client Web Service Description Language Web services webservices writing singleton design pattern in Java WSDL Xml xml parsers