Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Comparator Interface in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 317
    Comment on it

    Hi,
    Here I am writing a simple way to use Comparator interface.

    We use Comparator interface to order the objects. First of all we need to import java.util package. It have two methods :

    1. Compare(Object o1, Object o2)
    2. equals(Object o).

    The main advantage of Comparator is that it facilitates multiple sorting sequence.

    Syntax:

    1. public int compare(Object o1,Object o2);

    It will compare o1 to o2.

    We have Collections class that provides sorting methods.

    We can use Comparator object with this method for sorting like this

    public void sort(List myList,Comparator com)

    Example of sorting the elements of List on the basis of age and name

    Employee.java

    1. class Employee{
    2.  
    3. int empId;
    4.  
    5. String name;
    6.  
    7. int age;

    8.  
    9. Employee (int empId,String name,int age){
    10.  
    11. this. empId = empId;
    12.  
    13. this.name=name;
    14.  
    15. this.age=age;
    16.  
    17. }
    18.  
    19. }
    20.  

    CompareByAge.java

    1. import java.util.*;
    2.  
    3. class CompareByAge implements Comparator{
    4.  
    5. public int Compare(Object obj1,Object obj2){
    6.  
    7. Employee emp1=( Employee)o1;
    8.  
    9. Employee emp2=( Employee)o2;
    10.  
    11. if(emp1.age== emp2.age)

    12.  
    13. return 0;
    14.  
    15. else if(emp1.age> emp2.age)
    16.  
    17. return 1;
    18.  
    19. else
    20.  
    21. return -1;
    22.  
    23. }
    24.  
    25. }
    26.  
    27.  

    CompareByName.java.

    1. import java.util.*;
    2.  
    3. class CompareByName implements Comparator{
    4.  
    5. public int Compare(Object o1,Object o2){
    6.  
    7. Employee emp1=( Employee)o1;
    8.  
    9. Employee emp2=( Employee)o2;

    10.  
    11. return emp1.name.compareTo(emp2.name);

    12.  
    13. }
    14.  
    15. }


    Main.java

    1. class Main{
    2.  
    3. public static void main(String args[]){

    4.  
    5. ArrayList empList=new ArrayList();

    6.  
    7. empList.add(new Employee(3,"BBB",25));
    8.  
    9. empList.add(new Employee (1,"DDD",24));
    10.  
    11. empList.add(new Employee (7,"AAA",28));

    12.  
    13. System.out.println("Sorting + + By + + Name");

    14.  
    15. Collections.sort(empList,new CompareByName ());

    16.  
    17. Iterator itr=al.iterator();
    18.  
    19. while(itr.hasNext()){
    20.  
    21. Employee emp=( Employee)itr.next();
    22.  
    23. System.out.println(emp.empId+" "+ emp.name+" "+ emp.age);
    24.  
    25. }

    26.  
    27. System.out.println("Sorting + + By + + Age");

    28.  
    29. Collections.sort(empList,new CompareByAge());

    30.  
    31. Iterator itr2= empList.iterator();
    32.  
    33. while(itr2.hasNext()){
    34.  
    35. Employee emp=(Employee)itr2.next();
    36.  
    37. System.out.println(emp.empId+" "+ emp.name+" "+ emp.age);
    38.  
    39. }

    40.  
    41. }

    42.  
    43. }

    Output

    1. Sorting + + By + + Name
    2. 7 AAA 28
    3. 3 BBB 25
    4. 1 DDD 24
    5. Sorting + + By + + Age
    6. 1 DDD 24
    7. 3 BBB 25
    8. 7 AAA 28

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: