Showing posts with label hashCode. Show all posts
Showing posts with label hashCode. Show all posts

Comparing Objects by values equality by overriding equal / hashCode() method with example.

Basically , Obj1.equals(Obj2) returns true , if Obj1 and Obj2 refer to the same object that means the references of two Objects (Obj1 & Obj2 ) point to the same object which is equal to if (Obj1==Obj2) . This is the default implementation of the equal method of an Object. But we need to compare one or more values of the two Objects. This can be done by overriding the equals method (i.e boolean equals(Object obj) ) . By overriding equal method , you can write your own logic to decide wheather two objects of the same class are equal or not that is the implementor decides which values should be equal to consider two objects to be equal. In our example , in Student class, the two student objects equality is decided by the total marks.
In the following program, Student class is defined with fields of name , Mark1 , Mark2, Mark3 and One constructor to assign values and public methods to access fields. We can create one or more student objects from Student class. Our objective is to check two student objects are equal or not based on the total value of 3 marks .So we have overrided the equal method by writing the logic to compare total marks of two objects of same class. The hashCode method has to be redefined as we are redefining the equals method . The concept is the Equal objects must have equal hash codes.

class Student
{
private String name;
private int mark1;
private int mark2;
private int mark3;
public Student(String name, int m1, int m2, int m3)
{
this.name=name;
this.mark1=m1;
this.mark2=m2;
this.mark3=m3;
}
public String getName() { return this.name; }
public int getMark1() {return this.mark1;}
public int getMark2() { return this.mark2; }
public int getMark3() { return this.mark3; }

public void printValues()
{
System.out.println("name : " + this.name);
System.out.println("M1 : " + this.mark1);
System.out.println("M2 : " + this.mark2);
System.out.println("M3 : " + this.mark3);
}
public boolean equals(Object obj)
{
boolean eq=false;
//if both object's reference is equal means , objects are equal
if(this == obj) return true;
if (obj instanceof Student)
{
Student student = (Student) obj;
int tot1=student.getMark1() + student.getMark2() + student.getMark3();
int tot2=this.getMark1() + this.getMark2() + this.getMark3();
if (tot1==tot2) eq=true;
//the following line checks name also to decide Object's equality
// if ((tot1==tot2) &&( student.getName().equals(this.getName())) ) eq=true;
return eq;
}
else
{
return false;
}
}
//hashCode() is overrided.
public int hashCode()
{
int hash = 19;
String tot1=Integer.toString(mark1+mark2+mark3);
hash = 37 * hash + tot1.hashCode();
//hashCode() of 'name' field is also considered in the below line. If equal method use 'name' field means , hashCode() also has to use.
//hash = 37 * hash + name.hashCode() +tot1.hashCode();
return hash;
}
}

//Class to create Students objects and compare two student objects for equality using overrided equal method

class OverrideEqual
{
public static void main(String args[])
{
Student stu1 = new Student("ABC", 50, 30, 21);
Student stu2 = new Student("XYZ", 50, 30, 21);
stu1.printValues();
System.out.println("");
stu2.printValues();
if ( stu1.equals(stu1) ) System.out.println("Both object references point to the same object"); else System.out.println("Both are different objects");
if ( stu1.equals(stu2 ) ) System.out.println("Both student are equal"); else System.out.println("Both students are not equal");
if ( stu2.equals(stu1 ) ) System.out.println("Two students are equal"); else System.out.println("Two student are not equal");
if (stu1.hashCode()==stu2.hashCode()) System.out.println("Both Student Object Hashcode are equal"); else System.out.println("Both Student Object Hashcode are not equal");

}
}


//Output :

Read more...

 
Copyright 2011 Java Online