Introduction :A Comparator object is capable of comparing two different objects. It should implement java.util.Comparator interface. ![]() Comparator interface has one method Compare(Object o1, Object o2). The return values of the compare() method of Comparator are similar to the compareTo() method of Comparable.
Problem :Create a program that sort Employees’ list by
2. Employee ID in descending order 3. Employee Name in ascending order Solution :Different kind of sorting will be done by java.util.Comparator interface. There will be a class Employee that keeps employee’s ID and Name. Employees are sorted in 3 three different order so there will be three Comparator classes will be implemented
Program :Step 1 - Coding(1)Employee Class /* * Keeps Employee information. */ public class Employee { public int id; public String name; public Employee() {}; public Employee(int id, String name) { this.name = name; public int getId() { public void setId(int id) { public String getName() {
} (2)CompareEmployeeByID Class import java.util.Comparator; /* */ public class CompareEmployeeByID implements Comparator<Employee> { /* public int compare(Employee emp1, Employee emp2) { } (3)CompareEmployeeByIDDesc Class /* import java.util.Comparator; /** * Compare Employee IDs and returns * 1 : If Employee One ID is less than Employee ID Two * 0 : If both Employee IDs are equal * -1 : If Employee One ID is greater than Employee ID Two */ public int compare(Employee emp1, Employee emp2) { } (4)CompareEmployeeByName Class import java.util.Comparator; /* public class CompareEmployeeByName implements Comparator<Employee> { /** * Compare Employee Names and returns
} (5)SortEmployeeListByComparator Class /* * A program to compare and sort Employee objects using multiple Comparator implementation.
public static void main(String[] args) throws IOException { Employee e1 = new Employee(3, "Ram lal");
ArrayList list = new ArrayList(); list.add(e1);
Collections.sort(list, new CompareEmployeeByID()); Iterator<Employee> itr = list.iterator();
emp = itr.next(); }
while (itr.hasNext()) { emp = itr.next();
emp = itr.next(); } } } Step 2 - DeploymentCreate a folder 'c:\sunilos'. Create or copy Employee.java,CompareEmployeeID.java,CompareEmployeeIDDesc.java,CompareEmployeeByName.java,SortEmployeeListByComparator. into 'c:\sunilos' folder. Open your command prompt and go to 'c:\sunilos' Compile Employee.java,CompareEmployeeID.java,CompareEmployeeIDDesc.java,CompareEmployeeByName.java,SortEmployeeListByComparator.java with help of javac *.java java command. Command will create class file in the same folder. Congratulations!! your Java program is ready to serve. Step 3 - TestingMake sure you are on Command Prompt under c:\sunilos directory Now start your java program from command prompt with help of java SortEmployeeListByComparator command. Output :****Employee List By ID ( Ascending )**** ID Name 1 Shanti Lal ****Employee List By ID ( Descending )**** ID Name 3 Ram lal ****Employee List BY Name ( Ascending )**** ID Name 2 Chunni Lal FAQQ.1 What is Comparator and what it is used for?A : Comparator : A Comparator object is capable of comparing two different objects. instances. This comparator class must implement the java.util.Comparator interface . Comparator interface is used when an extra logic is required to sort the objects. One need to override compare(Object obj1, Object obj2) method. Q.2 When to use Comparator ?A : By using Comparator, you could instead make different comparators, one that compares Employee objects by name, and another one that compares them by ID. If you want to sort by name, you use the first Comparator object, and if you want to sort by ID you use the other Comparator object. Q.3 How many Comparators you can create for a single class say "Employee" ?A : We can create multiple Comparators for Employee class. If Employee object need to sort on Name and ID attributes then we will create two Comparators one for Name and second for ID. That means if Employee class need to sort on 4 attributes then we will create 4 Comparators, one for each attribute. |