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
    • 297
    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:

    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

    class Employee{ 
    int empId;
    String name;

    int age;
    Employee (int empId,String name,int age){
    this. empId = empId;
    this.name=name;
    this.age=age;
    }
    }

    CompareByAge.java

    import java.util.*; 
    class CompareByAge implements Comparator{
    public int Compare(Object obj1,Object obj2){
    Employee emp1=( Employee)o1;
    Employee emp2=( Employee)o2;

    if(emp1.age== emp2.age)
    return 0;
    else if(emp1.age> emp2.age)
    return 1;
    else
    return -1;
    }
    }

    CompareByName.java.

    import java.util.*; 
    class CompareByName implements Comparator{
    public int Compare(Object o1,Object o2){
    Employee emp1=( Employee)o1;
    Employee emp2=( Employee)o2;

    return emp1.name.compareTo(emp2.name);
    }
    }


    Main.java

    class Main{ 
    public static void main(String args[]){

    ArrayList empList=new ArrayList();
    empList.add(new Employee(3,"BBB",25));
    empList.add(new Employee (1,"DDD",24));
    empList.add(new Employee (7,"AAA",28));

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

    Collections.sort(empList,new CompareByName ());
    Iterator itr=al.iterator();
    while(itr.hasNext()){
    Employee emp=( Employee)itr.next();
    System.out.println(emp.empId+" "+ emp.name+" "+ emp.age);
    }

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

    Collections.sort(empList,new CompareByAge());
    Iterator itr2= empList.iterator();
    while(itr2.hasNext()){
    Employee emp=(Employee)itr2.next();
    System.out.println(emp.empId+" "+ emp.name+" "+ emp.age);
    }

    }
    }

    Output

    Sorting  +  +  By + + Name 
           7 AAA 28 
           3 BBB 25 
           1 DDD 24 
    Sorting  +  +  By + + Age 
           1 DDD 24 
           3 BBB 25 
           7 AAA 28 

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: