Can you provide me an example of Comparator Interface?
A comparator object is capable of comparing two different objects. Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap).
Here is an example that compares the object of class Person by age.
import java.util.*; class Person{ int age; String name; public void setAge(int age){ this.age=age; } public int getAge(){ return this.age; } public void setName(String name){ this.name=name; } public String getName(){ return this.name; } } class AgeComparator implements Comparator{ public int compare(Object ob1, Object ob2){ int ob1Age = ((Person)ob1).getAge(); int ob2Age = ((Person)ob2).getAge(); if(ob1Age > ob2Age) return 1; else if(ob1Age < ob2Age) return -1; else return 0; } } public class ComparatorExample{ public static void main(String args[]){ Person person[] = new Person[3]; person[0] = new Person(); person[0].setAge(35); person[0].setName("A"); person[1] = new Person(); person[1].setAge(30); person[1].setName("B"); person[2] = new Person(); person[2].setAge(32); person[2].setName("C"); System.out.println("Order of person before sorting is"); for(int i=0; i < person.length; i++){ System.out.println( person[i].getName() + "\t" + person[i].getAge()); } Arrays.sort(person, new AgeComparator()); System.out.println("\n\nOrder of person after sorting by person age is"); for(int i=0; i < person.length; i++){ System.out.println( person[i].getName() + "\t" + person[i].getAge()); } } }
public class Employee{
int empId; String eName; public Employee(int empId, String eName) { this.empId = empId; this.eName = eName; } @Override public String toString() { return "ID:"+empId+"NAME:"+eName; }
}
public Student(String sName, String sDept) { this.sName = sName; this.sDept = sDept; }
@Override public String toString() {
return "NAME:"+sName+"DEPT:"+sDept;
}
}
public class CommonSort implements Comparator{
@Override public int compare(Object obj1, Object obj2) { int i=0; if((obj1!=null && obj2!=null) && obj1 instanceof Employee && obj2 instanceof Employee){ Employee e1=(Employee)obj1; Employee e2=(Employee)obj2; i=e1.eName.compareTo(e2.eName); System.out.println(e1.eName+"--"+e2.eName+"--"+i); } if((obj1!=null && obj2!=null) && obj1 instanceof Student && obj2 instanceof Student){ Student s1=(Student)obj1; Student s2=(Student)obj2; i=s1.sDept.compareTo(s2.sDept); } return i; }
import java.util.Iterator; import java.util.TreeSet;
public class EmpMain {
/** * @param args */ public static void main(String[] args) { CommonSort cs=new CommonSort(); TreeSet<Employee> ts =new TreeSet<Employee>(cs); ts.add(new Employee(123,"vijay")); ts.add(new Employee(98,"kumar")); ts.add(new Employee(100,"abhi")); ts.add(new Employee(50,"dhruv")); ts.add(new Employee(150,"sasi")); for (Iterator iterator = ts.iterator(); iterator.hasNext();) { Employee employee = (Employee) iterator.next(); System.out.println(employee); } System.out.println("-----------------------------------"); TreeSet<Student> ts2 =new TreeSet<Student>(cs); ts2.add(new Student("kumar","cse")); ts2.add(new Student("abhi","it")); ts2.add(new Student("vijay","mec")); ts2.add(new Student("bhavani","ece")); ts2.add(new Student("sasi","eee")); for (Iterator iterator = ts2.iterator(); iterator.hasNext();) { Student student = (Student) iterator.next(); System.out.println(student); } }
}
the above example is for the statement:
Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap).
output:
kumar--vijay---11 abhi--vijay---21 abhi--kumar---10 dhruv--kumar---7 dhruv--abhi--3 sasi--kumar--8 sasi--vijay---3 ID:100NAME:abhi ID:50NAME:dhruv ID:98NAME:kumar ID:150NAME:sasi ID:123NAME:vijay NAME:kumarDEPT:cse NAME:bhavaniDEPT:ece NAME:sasiDEPT:eee NAME:abhiDEPT:it NAME:vijayDEPT:mec
Ads