Importance of equals and hashcode method override:
Both equals and hashcode method present in java.lang.Object, hence these methods available for all java classes.
When comparing the objects, these methods are used.
Default implentation:
equals -> checks two objects are pointing same memory location (== check)
hashcode -> returns an integer representation of the object memory address. By default, this method returns a random integer that is unique for each instance. This integer might change between several executions of the application and won't stay the same.
Note :
"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."
Example:
We will define Student class with and without overriding equals & hashcode.
Student.java
public class Student {
private int id;
private String name;
public Student(int id, String name) {
Student1.java -> Here we are override equals and hashcode methods.
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.name = name;
this.id = id;
}
// getters and setter
@Override
public boolean equals(Object o) { // 3 if's and final comparison
if (o == null ) {
return false;
}
if(!(o instanceof Student1)) {
return false;
}
if(o == this) {
return true;
}
Student1 student = (Student1)o;
return this.getId() == student.getId() && this.getName() == student.getName();
}
Test:
package com.raj.basics;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import com.raj.model.Student;
import com.raj.model.Student1;
public class EqualsHashCodeOverride {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(); // List will allow duplication and maintains order.
List<Student1> student1s = new ArrayList<>();
Student student1 = new Student(1,"Raj kumar");
Student student2 = new Student(1,"Raj kumar");
Student1 student11 = new Student1(1,"Raj kumar");
Student1 student12 = new Student1(1,"Raj kumar");
students.add(student1);students.add(student2);
student1s.add(student11);student1s.add(student12);
System.out.println("Students size : "+ students.size()); //2
System.out.println("Student1s size : "+ student1s.size()); //2
System.out.println("Students check : "+ students.contains(new Student(1,"Raj kumar"))); //false// Object's equals method considered for comparison(just memory location check)
System.out.println("Student1s check : "+ student1s.contains(new Student1(1,"Raj kumar"))); //true// Student1's equal method considered for comparison (deep check - logic done by us)
HashSet <Student> studentSet = new HashSet<>(); // HashSet wont allow duplicates.It will use hashcode for storing elements. Hashcode will vary for objects.
HashSet <Student1> student1Set = new HashSet<>();
studentSet.add(student1);studentSet.add(student2);
student1Set.add(student11);student1Set.add(student12);
System.out.println("Students set size : "+ studentSet.size()); // 2 because Object's hashcode taken for comparison
System.out.println("Student1s set size : "+ student1Set.size()); // 1 because Student1's equal method taken for consideraton.
System.out.println("HashSet contains = " + studentSet.contains(new Student(1, "Raj kumar"))); // false
System.out.println("HashSet contains = " + student1Set.contains(new Student1(1, "Raj kumar"))); // true
}
}
Output:
Students size : 2
Student1s size : 2
Students check : false
Student1s check : true
Students set size : 2
Student1s set size : 1
HashSet contains = false
HashSet contains = true
Both equals and hashcode method present in java.lang.Object, hence these methods available for all java classes.
When comparing the objects, these methods are used.
Default implentation:
equals -> checks two objects are pointing same memory location (== check)
hashcode -> returns an integer representation of the object memory address. By default, this method returns a random integer that is unique for each instance. This integer might change between several executions of the application and won't stay the same.
Note :
"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."
Example:
We will define Student class with and without overriding equals & hashcode.
Student.java
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.name = name;
this.id = id;
}
// getters and setter
}
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.name = name;
this.id = id;
}
// getters and setter
@Override
public boolean equals(Object o) { // 3 if's and final comparison
if (o == null ) {
return false;
}
if(!(o instanceof Student1)) {
return false;
}
if(o == this) {
return true;
}
Student1 student = (Student1)o;
return this.getId() == student.getId() && this.getName() == student.getName();
}
}
}Test:
package com.raj.basics;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import com.raj.model.Student;
import com.raj.model.Student1;
public class EqualsHashCodeOverride {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(); // List will allow duplication and maintains order.
List<Student1> student1s = new ArrayList<>();
Student student1 = new Student(1,"Raj kumar");
Student student2 = new Student(1,"Raj kumar");
Student1 student11 = new Student1(1,"Raj kumar");
Student1 student12 = new Student1(1,"Raj kumar");
students.add(student1);students.add(student2);
student1s.add(student11);student1s.add(student12);
System.out.println("Students size : "+ students.size()); //2
System.out.println("Student1s size : "+ student1s.size()); //2
System.out.println("Students check : "+ students.contains(new Student(1,"Raj kumar"))); //false// Object's equals method considered for comparison(just memory location check)
System.out.println("Student1s check : "+ student1s.contains(new Student1(1,"Raj kumar"))); //true// Student1's equal method considered for comparison (deep check - logic done by us)
HashSet <Student> studentSet = new HashSet<>(); // HashSet wont allow duplicates.It will use hashcode for storing elements. Hashcode will vary for objects.
HashSet <Student1> student1Set = new HashSet<>();
studentSet.add(student1);studentSet.add(student2);
student1Set.add(student11);student1Set.add(student12);
System.out.println("Students set size : "+ studentSet.size()); // 2 because Object's hashcode taken for comparison
System.out.println("Student1s set size : "+ student1Set.size()); // 1 because Student1's equal method taken for consideraton.
System.out.println("HashSet contains = " + studentSet.contains(new Student(1, "Raj kumar"))); // false
System.out.println("HashSet contains = " + student1Set.contains(new Student1(1, "Raj kumar"))); // true
}
}
Students size : 2
Student1s size : 2
Students check : false
Student1s check : true
Students set size : 2
Student1s set size : 1
HashSet contains = false
HashSet contains = true
No comments:
Post a Comment