How to Override equals and hashcode method
equals method: equals is a method check equality of two object based on content of object
if two object have same data then it return true otherwise it return false.
hashcode method: hashcode method return an integer value which represent memory location of object it can same for two unequal object . but is good practice have different value for each object .
Need of Override the equals and hashcode method
If not overriding the equlas and hashcode method then we can not have or
store non duplicate object in HashSet or we can't use our class object as a key in HashMap
Here is the contract, from the java.lang.Object specialization:
1.Whenever it(hashcode) is invoked on the same object more than once during an execution of a Java application,
the hashCode method must consistently return the same integer, provided no information used in equals
comparisons on the object is modified. This integer need not remain consistent from one execution of
an application to another execution of the same application.
2.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.
3.It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling
the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be
aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
Employee class with overridden equlas and hashcode method
package in.jk.example;
public class Employee {
private int empId;
private String name;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// Override of hashcode
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + this.empId;
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
return result;
}
// Override of equlas
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee employee = (Employee) obj;
if (this.empId != employee.empId)
return false;
if (this.name == null) {
if (employee.name != null)
return false;
} else if (!this.name.equals(employee.name))
return false;
return true;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + "]";
}
}
package in.jk.example;
import java.util.HashSet;
public class Application {
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.setEmpId(1);
emp1.setName("JK");
Employee emp2 = new Employee();
emp2.setEmpId(1);
emp2.setName("JK");
Employee emp3 = new Employee();
emp3.setEmpId(1);
emp3.setName("JK");
Employee emp4 = new Employee();
emp4.setEmpId(2);
emp4.setName("jk");
Employee emp5 = new Employee();
emp5.setEmpId(4);
emp5.setName("johny");
Employee emp6 = new Employee();
emp6.setEmpId(4);
emp6.setName("johny");
Employee emp7 = new Employee();
emp7.setEmpId(5);
emp7.setName("johny kumar");
Employee emp8 = new Employee();
emp8.setEmpId(8);
emp8.setName("john");
HashSet<Employee> employees = new HashSet<Employee>();
employees.add(emp1);
employees.add(emp2);
employees.add(emp3);
employees.add(emp4);
employees.add(emp5);
employees.add(emp6);
employees.add(emp7);
employees.add(emp8);
System.out.println("Size : "+employees.size());
employees.stream().forEach(System.out::println);
}
}
No comments:
Post a Comment