Sometime or always , people keep asking
1) Why we need equals method in model object?
2) Why we need hashCode method in model object?
Rather having answer , let's start with example directly:
public class Person {
private String name;
private String mobileNo;
public Person(String name, String mobileNo) {
this.name = name;
this.mobileNo = mobileNo;
}
//getter and setter
}
//let's create objects
Person person1= new Person("Chandu","1234567890");
Person person2= new Person("Chandu","1234567890");
//Even a lame will say,these two objects are same, let's check
if(person1.equals(person2)){
System.out.println("Person are same in equal Method");
}
else
{
System.out.println("Person are NOT same.");
}
//Output is
Person are NOT same.
//what the hell,how come?
Now Java doesn't see the obvious reason , the way we see it.We have to tell ,look, these two objects are same and don't apply your equals() as i m gonna write mine.
Add this under Person.java
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (mobileNo == null) {
if (other.mobileNo != null)
return false;
} else if (!mobileNo.equals(other.mobileNo))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
To cut long story short , this equals () says if this object name and mobile no are same with other object , objects are equal.
Lets run one more time and we get output:
Person are same in equal Method. //Nailed it.
So when we are writing our own model , we also need to give custom implementations for equals() based on our requirements.
So far , so good.
Let's tweak a little.
Add some piece of code in main method
Set<Person> personSet = new HashSet<Person>();
//A Set of person,Set doesn't allow duplicate entries.
//Means it will replace first object with second if they are equal.
personSet.add(person1);
personSet.add(person2);
//person1 and person2 in set, and we knew already ,these two objects were same.
System.out.println("Set size is " + personSet.size());
And output is :
Set size is 2
//What the freak, size should be same 1 , though we added two objects but they //were equal.
What went wrong?
A clue is ,Collection objects works on hash.
What is hash()?It's a way to represent an object in numerical order.
Let's add some more code to our main()
System.out.println("HashCode for person1 " + person1.hashCode());
System.out.println("HashCode for person1 " + person2.hashCode());
My system o/p is :
HashCode for person1 1704434230
HashCode for person1 1141736277
If hash is a way representation of an object and if above two objects were same,then hash codes would had been same. But that's not the case here.
So in JAVA ,there is a saying
If two objects are same by equals(), then hascode() for these objects should be same.
But in our case, it didn't happen.So what and where it went wrong?
This time we have to overwrite hashCode() in Person.java.
@Override
public int hashCode() {
final int prime = 31;//Always use prime number.
int result = 1;
result = prime * result
+ ((mobileNo == null) ? 0 : mobileNo.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
Now Let's run main()
And output is :
Person are same in equal Methods
HashCode for person1 -1532305399
HashCode for person1 -1532305399
Set size is 1
//Finally,Nailed it.
That's what we wanted.
Thumb Rule,there is a silent contract b/w hashCode() and equals() :
1) Why we need equals method in model object?
2) Why we need hashCode method in model object?
Rather having answer , let's start with example directly:
public class Person {
private String name;
private String mobileNo;
public Person(String name, String mobileNo) {
this.name = name;
this.mobileNo = mobileNo;
}
//getter and setter
}
//let's create objects
Person person1= new Person("Chandu","1234567890");
Person person2= new Person("Chandu","1234567890");
//Even a lame will say,these two objects are same, let's check
if(person1.equals(person2)){
System.out.println("Person are same in equal Method");
}
else
{
System.out.println("Person are NOT same.");
}
//Output is
Person are NOT same.
//what the hell,how come?
Now Java doesn't see the obvious reason , the way we see it.We have to tell ,look, these two objects are same and don't apply your equals() as i m gonna write mine.
Add this under Person.java
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (mobileNo == null) {
if (other.mobileNo != null)
return false;
} else if (!mobileNo.equals(other.mobileNo))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
To cut long story short , this equals () says if this object name and mobile no are same with other object , objects are equal.
Lets run one more time and we get output:
Person are same in equal Method. //Nailed it.
So when we are writing our own model , we also need to give custom implementations for equals() based on our requirements.
So far , so good.
Let's tweak a little.
Add some piece of code in main method
Set<Person> personSet = new HashSet<Person>();
//A Set of person,Set doesn't allow duplicate entries.
//Means it will replace first object with second if they are equal.
personSet.add(person1);
personSet.add(person2);
//person1 and person2 in set, and we knew already ,these two objects were same.
System.out.println("Set size is " + personSet.size());
And output is :
Set size is 2
//What the freak, size should be same 1 , though we added two objects but they //were equal.
What went wrong?
A clue is ,Collection objects works on hash.
What is hash()?It's a way to represent an object in numerical order.
Let's add some more code to our main()
System.out.println("HashCode for person1 " + person1.hashCode());
System.out.println("HashCode for person1 " + person2.hashCode());
My system o/p is :
HashCode for person1 1704434230
HashCode for person1 1141736277
If hash is a way representation of an object and if above two objects were same,then hash codes would had been same. But that's not the case here.
So in JAVA ,there is a saying
If two objects are same by equals(), then hascode() for these objects should be same.
But in our case, it didn't happen.So what and where it went wrong?
This time we have to overwrite hashCode() in Person.java.
@Override
public int hashCode() {
final int prime = 31;//Always use prime number.
int result = 1;
result = prime * result
+ ((mobileNo == null) ? 0 : mobileNo.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
Now Let's run main()
And output is :
Person are same in equal Methods
HashCode for person1 -1532305399
HashCode for person1 -1532305399
Set size is 1
//Finally,Nailed it.
That's what we wanted.
Thumb Rule,there is a silent contract b/w hashCode() and equals() :
- 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.
Enjoy Equal and Hashing!!!!.