equals() and hashCode() methods of Object class are used to check the identity of objects. It is recommended that if you override one method then you must override other also. There is a relation or contract between these two methods that must be maintained all the time. That contract can be defined like this,
“If two objects are equal according to equals() method then they must have same hashcode() value. But, reverse is not always true.”
To maintain this contract all the time, it is recommended that you override both the methods. If you override equals() method and you don’t override hashCode() method, then two equal objects according to equals() method will have different hashCode values. This breaks the above contract. Therefore, you have to override equals() and hashCode() methods together.
Below example shows how to override equals() and hashCode() methods together.
class Person
{
String firstName;
String lastName;
int age;
public Person(String firstName, String lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
//Overriding equals() method
@Override
public boolean equals(Object obj)
{
Person person = (Person) obj;
return this.firstName == person.firstName && this.lastName == person.lastName && this.age == person.age;
}
//Overriding hashCode() method
@Override
public int hashCode()
{
int hashCode = 0;
hashCode += firstName.hashCode();
hashCode += lastName.hashCode();
hashCode += Integer.toString(age).hashCode();
return hashCode;
}
}
public class HashCodeMethodDemo
{
public static void main(String[] args)
{
//Creating two objects of Person with same content
Person p1 = new Person("Robin", "Smith", 42);
Person p2 = new Person("Robin", "Smith", 42);
//Now, comparing p1 and p2 using equals() method
//It will return true as both the objects have same content
System.out.println(p1.equals(p2)); //Output : true
//They will also have same hashcode value.
System.out.println(p1.hashCode()); //Output : 159138795
System.out.println(p2.hashCode()); //Output : 159138795
}
}
Some Extra Points About hashCode() method :
“If two objects are equal according to equals() method then they must have same hashcode() value. But, reverse is not always true.”
To maintain this contract all the time, it is recommended that you override both the methods. If you override equals() method and you don’t override hashCode() method, then two equal objects according to equals() method will have different hashCode values. This breaks the above contract. Therefore, you have to override equals() and hashCode() methods together.
Below example shows how to override equals() and hashCode() methods together.
class Person
{
String firstName;
String lastName;
int age;
public Person(String firstName, String lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
//Overriding equals() method
@Override
public boolean equals(Object obj)
{
Person person = (Person) obj;
return this.firstName == person.firstName && this.lastName == person.lastName && this.age == person.age;
}
//Overriding hashCode() method
@Override
public int hashCode()
{
int hashCode = 0;
hashCode += firstName.hashCode();
hashCode += lastName.hashCode();
hashCode += Integer.toString(age).hashCode();
return hashCode;
}
}
public class HashCodeMethodDemo
{
public static void main(String[] args)
{
//Creating two objects of Person with same content
Person p1 = new Person("Robin", "Smith", 42);
Person p2 = new Person("Robin", "Smith", 42);
//Now, comparing p1 and p2 using equals() method
//It will return true as both the objects have same content
System.out.println(p1.equals(p2)); //Output : true
//They will also have same hashcode value.
System.out.println(p1.hashCode()); //Output : 159138795
System.out.println(p2.hashCode()); //Output : 159138795
}
}
Some Extra Points About hashCode() method :
- Whenever hashCode() method is called on the same object more than once during an execution of Java application, it must return consistently same value, provided object is not modified.
- The value returned by hashCode() method may or may not change from one execution to another execution of the same application.
- For most of the time, hashCode() method returns distinct integer for distinct objects if it is not overrided.
- If two objects are not equal according to equals() method, then it is not necessary that their hashcode must also be different. But, it is a good practice to return different integers for different objects if hashCode() method is overrided.
- If hashCode() method is overrided in a class and you want to know the original hashcode of an object of that class, then use System.identityHashCode() method. It returns hashcode of an object as same as that of default hashCode() method irrespective of whether hashCode() method is overrided or not.
- Hashcode of a null object is zero.
No comments:
Post a Comment