Java Comparator is used to compare two objects and place in a particular order.
The Comparator is an interface, it is available in java.util package and it contains two methods for comparing the objects.
compare(Object obj1, Object obj2) and equals(Object obj)
public abstract int compare(T paramT1, T paramT2);
public abstract boolean equals(Object paramObject);
These methods compare and sort objects based on our requirements like id, name, salary, and age etc..
Example for Comparator interface
Generally, by default TreeSet follow default sorting order by the Comparable interface and provides result in ascending order.
Here we are doing our customized sorting order i.e. descending order by implementing Comparator interface.
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
Set set=new TreeSet(new MyCompare());
set.add(30);
set.add(10);
set.add(5);
set.add(20);
set.add(0);
System.out.println(set);
}
}
class MyCompare implements Comparator{
@Override
public int compare(Object obj1, Object obj2) {
Integer i1=(Integer)obj1;
Integer i2=(Integer)obj2;
if(i1<i2){
return +1;
}
else if(i1>i2){
return -1;
}
else{
return 0;
}
}
}
It gives output as : [30, 20, 10, 5, 0]
Example for compare based on our requirements
Let’s check the example for sorting elements based on id and name
Employee.java
It contains two fields eid, ename and getter methods to get data and override toString() method for representing meaningful content.
public class Employee {
private int eid;
private String ename;
public Employee(int eid, String ename) {
super();
this.eid = eid;
this.ename = ename;
}
public int getEid() {
return eid;
}
public String getEname() {
return ename;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return eid+" "+ename;
}
}
CompareId.java
Sort the employee details based on the eid
import java.util.Comparator;
import java.util.TreeSet;
public class CompareId {
public static void main(String[] args) {
TreeSet set=new TreeSet(new ComparTest());
set.add(new Employee(101, "Mahi"));
set.add(new Employee(103, "Laxman"));
set.add(new Employee(105, "RK"));
set.add(new Employee(104, "shyam"));
set.add(new Employee(102, "Pawan"));
for(Object o:set){
System.out.println(o);
}
}
}
class ComparTest implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2) {
Integer i1=e1.getEid();
Integer i2=e2.getEid();
return -i1.compareTo(i2);
}
}
Ouput is based on Id:
105 RK
104 shyam
103 Laxman
102 Pawan
101 Mahi
CompareName.java
Sort employee details based on the ename
import java.util.Comparator;
import java.util.TreeSet;
public class CompareName {
public static void main(String[] args) {
TreeSet set=new TreeSet(new ComparTest());
set.add(new Employee(101, "Mahi"));
set.add(new Employee(103, "Laxman"));
set.add(new Employee(105, "RK"));
set.add(new Employee(104, "shyam"));
set.add(new Employee(102, "Pawan"));
for(Object o:set){
System.out.println(o);
}
}
}
class ComparTest implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2) {
String s1=e1.getEname();
String s2=e2.getEname();
return -s1.compareTo(s2);
}
}
Output is based on Names:
104 shyam
105 RK
102 Pawan
101 Mahi
103 Laxman
No comments:
Post a Comment