June 05, 2011

Java Comparator Example


  1. package examples;
  2. /*
  3. Java Comparator example.
  4. This Java Comparator example describes how java.util.Comparator
  5. interface is implemented to compare Java custom class objects.
  6. This Java Comparator is passed to Collection's sorting method
  7. (for example Collections.sort method) to perform sorting of Java custom
  8. class objects.
  9. */
  10. import java.util.*;
  11. /*
  12. java.util.Comparator interface declares two methods,
  13. 1) public int compare(Object object1, Object object2) and
  14. 2) boolean equals(Object object)
  15. */
  16. /*
  17. We will compare objects of the Employee class using custom comparators
  18. on the basis of employee age and name.
  19. */
  20. class Employee{
  21.    
  22.     private int age;    
  23.     private String name;
  24.    
  25.     public void setAge(int age){
  26.         this.age=age;    
  27.     }
  28.    
  29.     public int getAge(){
  30.         return this.age;    
  31.     }
  32.    
  33.     public void setName(String name){
  34.         this.name=name;    
  35.     }
  36.    
  37.     public String getName(){    
  38.         return this.name;    
  39.     }
  40. }
  41. /*
  42. User defined Java comparator.
  43. To create custom java comparator, implement Comparator interface and
  44. define compare method.
  45. The below given comparator compares employees on the basis of their age.
  46. */
  47. class AgeComparator implements Comparator{
  48.    
  49.     public int compare(Object emp1, Object emp2){
  50.    
  51.         /*
  52.          * parameter are of type Object, so we have to downcast it
  53.          * to Employee objects
  54.          */
  55.        
  56.         int emp1Age = ((Employee)emp1).getAge();        
  57.         int emp2Age = ((Employee)emp2).getAge();
  58.        
  59.         if(emp1Age > emp2Age)
  60.             return 1;
  61.         else if(emp1Age < emp2Age)
  62.             return -1;
  63.         else
  64.             return 0;    
  65.     }
  66.    
  67. }
  68. /*
  69. The below given comparator compares employees on the basis of their name.
  70. */
  71. class NameComparator implements Comparator{
  72.     public int compare(Object emp1, Object emp2){    
  73.         //parameter are of type Object, so we have to downcast it to Employee objects
  74.        
  75.         String emp1Name = ((Employee)emp1).getName();        
  76.         String emp2Name = ((Employee)emp2).getName();
  77.        
  78.         //uses compareTo method of String class to compare names of the employee
  79.         return emp1Name.compareTo(emp2Name);
  80.    
  81.     }
  82. }
  83. /*
  84. This Java comparator example compares employees on the basis of
  85. their age and name and sort them in that order.
  86. */
  87. public class JavaComparatorExample{
  88.    
  89.     public static void main(String args[]){
  90.        
  91.         //Employee array which will hold employees
  92.         Employee employee[] = new Employee[2];
  93.        
  94.         //set different attributes of the individual employee.
  95.         employee[0] = new Employee();
  96.         employee[0].setAge(40);
  97.         employee[0].setName("Joe");
  98.        
  99.         employee[1] = new Employee();
  100.         employee[1].setAge(20);
  101.         employee[1].setName("Mark");
  102.        
  103.         System.out.println("Order of employee before sorting is");
  104.         //print array as is.
  105.         for(int i=0; i < employee.length; i++){
  106.             System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
  107.         }
  108.        
  109.         /*
  110.         Sort method of the Arrays class sorts the given array.        
  111.         Signature of the sort method is,        
  112.         static void sort(Object[] object, Comparator comparator)
  113.        
  114.         IMPORTANT: All methods defined by Arrays class are static. Arrays class        
  115.         serves as a utility class.
  116.         */
  117.        
  118.         //Sorting array on the basis of employee age by passing AgeComparator
  119.         Arrays.sort(employee, new AgeComparator());
  120.         System.out.println("\n\nOrder of employee after sorting by employee age is");
  121.        
  122.         for(int i=0; i < employee.length; i++){
  123.             System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
  124.         }
  125.        
  126.         //Sorting array on the basis of employee Name by passing NameComparator
  127.         Arrays.sort(employee, new NameComparator());
  128.        
  129.         System.out.println("\n\nOrder of employee after sorting by employee name is");    
  130.         for(int i=0; i < employee.length; i++){
  131.             System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
  132.         }
  133.    
  134.     }
  135. }
  136. /*
  137. OUTPUT of the above given Java Comparable Example would be :
  138. Order of employee before sorting is
  139. Employee 1 name :: Joe, Age :: 40
  140. Employee 2 name :: Mark, Age :: 20
  141. Order of employee after sorting by employee age is
  142. Employee 1 name :: Mark, Age :: 20
  143. Employee 2 name :: Joe, Age :: 40
  144. Order of employee after sorting by employee name is
  145. Employee 1 name :: Joe, Age :: 40
  146. Employee 2 name :: Mark, Age :: 20
  147. */

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts