©2018 by javanotes.

Search
  • Madhu Reddy

Java equals() and hashCode()

Java equals() and hashCode() are two very important methods defined in Object Class.


equals() Method:


The default equals() method present in Object class checks for the memory reference of the both objects and returns true only if both the objects are pointing to the same memory location.


obj1.equals(obj2) returns true only if both obj1 and obj2 are pointing to the same memory location.


hashCode() Method:


The default hashCode() method returns the hash code value of the object.

If two objects are equal in java then there hashCode() values must be equal.

but if two hashCode() values are equal then those two objects may or may not be equal.



When two override equals() and hashCode() methods??


let’s take an example where we have an "Employee” object similar to below.


@Getter

@Setter

public class Employee {

private int id;

private String lname;

private String fname;

}


Now let's look at below code snippet.


Employee e1= new Employee();

e1.setId(123);

e1.setFname("java");

e1.setLname("notes");


Employee e2= new Employee();

e2.setId(123);

e2.setFname("java");

e2.setLname("notes");

e1.equals(e2);


Now technically both the above Employee objects should be same and e1.equals(e2) should return true.


But that is not the case here in the above example.


since the Employee class will now use the default implementations of equals() method from the Object class, the memory references of the e1 and e2 will be checked here and it will return false.


e1 would be something like "Employee@312b1dae" and e2 would be something like "Employee@7530d0a" and hence they would not be equal.


but in real- time this is not we would have expected or not what we would have wanted.


That is the reason for the custom objects like the one above we need to override the default implementation of the Object class for equals() method.


Note: It does not give you any compilation error or any exception. But to maintain the rules between equals() and hashCode() we need to override.


You can override the equals() method something like this.


@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

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

return false;

final Employee other = (Employee) obj;

if (fname == null) {

if (other.fname != null)

return false;

} else if (!fname.equals(other.fname))

return false;

if (id != other.id)

return false;

if (lname == null) {

if (other.lname != null)

return false;

} else if (!lname.equals(other.lname))

return false;

return true;

}


There are some of the things that we need to consider while overriding the equals method in java.


1) obj1.equals(obj1) should always return true.

2) If obj1.equals(obj2) then obj2.equals(obj1) should always return true.

3)If obj1.equals(obj2), obj2.equals(obj3) then obj1.equals(obj3) must return true.


If we only override equals() method and doesn’t override the hashCode() method then for two objects which are equal the hashCode() values might not be equal since the hashCode() implementation is from Object class. This violates our statement that if two objects are equal then there hashCode() values must be equal.


Hence in java it a contract that if you override equals() method you have to override hashCode() method.


Hence similar to overriding the equals() method we can override the hashCode() method like below.


@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((fname == null) ? 0 : fname.hashCode());

result = prime * result + id;

result = prime * result + ((lname == null) ? 0 : lname.hashCode());

return result;

}


here are some of the things that we need to consider while overriding the hashCode() method:


1) The value of the hashCode() may change only if the property that is been involved in the hashCode() method changed.

2) If two objects are equal then there hashCode() values must be equal.


Now After overriding both the equals() and hashCode() methods.

e1.equals(e2) should return true as expected.


This is the importance of hashCode() and equals() method with very small example.

hashCode() implementation becomes more important when we are putting this objects in hashTable().