- Comparison between two objects is a very important in object oriented programming.
- Two objects with same values for their field is never same.But if it is required to equalize two object if their fields are same then we need to override equal and hash code methods.
Example
Student class:-
package com.sandeep.object.compare;
public class Student {
private String name;
private int roll;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
}
Create Objects and Test:-
package com.sandeep.object.compare;
public class ObjectComparisonTest {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.setName("Sandeep");
s1.setRoll(2);
s2.setName("Sandeep");
s2.setRoll(2);
boolean flagEqual;
flagEqual = s1.equals(s2);
System.out.print("flagEqual : " + flagEqual + "n");//output : false
}
}
Output:-
flagEqual : false
Overriding Equal and Hash Code
package com.sandeep.object.compare;
public class Student {
private String name;
private int roll;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + roll;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (roll != other.roll)
return false;
return true;
}
}
Create Objects and Test:-
package com.sandeep.object.compare;
public class ObjectComparisonTest {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.setName("Sandeep");
s1.setRoll(2);
s2.setName("Sandeep");
s2.setRoll(2);
boolean flagEqual;
flagEqual = s1.equals(s2);
System.out.print("flagEqual : " + flagEqual + "n");//output : true
}
}
Output:-
flagEqual : true
Sorting Objects (Ordering)
- Sorting object can be done by implementing Comparator interface.
- The java.util.Comparator must be implemented and Compare method should be overriden by a custom comparator class.
Implementing Comparator For Student:-
package com.sandeep.object.compare;
import java.util.Comparator;
public class StudentComparator implements Comparator{
@Override
public int compare(Student s1, Student s2) {
return s1.getRoll()-s2.getRoll();
}
}
Testing the Student Comparator:-
package com.sandeep.object.compare;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ObjectComparisonTest {
public static void main(String[] args) {
ListlistOfStudent = new ArrayList ();
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Student s4 = new Student();
Student s5 = new Student();
s1.setName("Sandeep");
s1.setRoll(5);
s2.setName("Sangeeta");
s2.setRoll(3);
s3.setName("Sumanta");
s3.setRoll(4);
s4.setName("Rohan");
s4.setRoll(1);
s5.setName("Surabhi");
s5.setRoll(2);
listOfStudent.add(s1);
listOfStudent.add(s2);
listOfStudent.add(s3);
listOfStudent.add(s4);
listOfStudent.add(s5);
Collections.sort(listOfStudent, new StudentComparator());
for (Student student : listOfStudent) {
System.out.print(student.getName() + " : " + student.getRoll()+ "n");
}
}
}
Output:-
Rohan : 1
Surabhi : 2
Sangeeta : 3
Sumanta : 4
Sandeep : 5
Other Methods:-
Collections.rotate(listOfStudent, 1);
Output:-
Sandeep : 5
Rohan : 1
Surabhi : 2
Sangeeta : 3
Sumanta : 4