Program to sort Employee list on basis of name and id in ascending order by implementing Comparator interface in inner and static inner class and overriding its compare method

ComparatorName (inner class of Employee class) and ComparatorId (static inner class of Employee class) are two classes which will implement Comparator to provide sorting on basis of name and id respectively.


How to use inner and static inner class?
For accessing non-static inner class we need to create instance of top level class and then instance of inner class.
For accessing static inner class we need not to create instance of top level class neither of static inner class.


new Employee().new ComparatorName()  >using inner class
new Employee  .    ComparatorId()    >using static inner

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Employee{
   String name;
   String id;
  
   public Employee() {}
  
   public Employee(String name, String id) {
       this.name = name;
       this.id = id;
   }
  
  
   @Override
   public String toString() {
       return "Employee{" + "name=" + name + ", id=" + id  + '}';
   }
   //Inner class
   class ComparatorName  implements Comparator<Employee>{
      @Override
       public int compare(Employee obj1, Employee obj2) {
          //sort Employee on basis of name(ascending order)
          return obj1.name.compareTo(obj2.name);
       }
   }
  
   //static Inner class
   static class ComparatorId  implements Comparator<Employee>{
      @Override
       public int compare(Employee obj1, Employee obj2) {
          //sort Employee on basis of id(ascending order)
          return obj1.id.compareTo(obj2.id);
       }
   }
}
/**
* @author AnkitMittal
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class
*/
public class ComparatorUsage {
   public static void main(String[] args) {
       Employee emp1=new Employee("sam","4");
       Employee emp2=new Employee("amy","2");
       Employee emp3=new Employee("brad","1");

       ArrayList<Employee> list=new ArrayList<Employee>();
       list.add(emp1);
       list.add(emp2);
       list.add(emp3);
     
       System.out.println("list Before sorting : \n"+list);
       Collections.sort(list,new Employee().new ComparatorName());
       System.out.println("\nlist after sorting on basis of name(ascending order), "
                 + "using inner class : \n"+ list);
       Collections.sort(list,new Employee.ComparatorId());
       System.out.println("\nlist after sorting on basis of id(ascending order), "
                 + "using static inner class : \n"+list);
      
   }
}
/*OUTPUT
list Before sorting :
[Employee{name=sam, id=4}, Employee{name=amy, id=2}, Employee{name=brad, id=1}]
list after sorting on basis of name(ascending order) :
[Employee{name=amy, id=2}, Employee{name=brad, id=1}, Employee{name=sam, id=4}]
list after sorting on basis of id(ascending order) :
[Employee{name=brad, id=1}, Employee{name=amy, id=2}, Employee{name=sam, id=4}]
*/

No comments:

Post a Comment