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
    • 549
    Comment on it

    Comparator:- Comparator interface  found in java.util package and it contains equals() and compare() method. It use the multiple sorting technique to write our own comparison logic and is used to sort the elements on the basis of any datamember of a class like rollno, name, age etc.

    Example:-

    Employee.java

    package com.comparator;
    
    import java.util.*;
    
    public class Employee{
    
    	int empId;  
    	String name;  
    	  
    	Employee(int empId,String name){  
    	this.empId=empId;  
    	this.name=name;  
    	}  
    
    }
    

    EmpIdComparator.java :- This Class is used to sort the elements on the basis of Employee Id.

    package com.comparator;
    
    import java.util.Comparator;
    
    public class EmpIdComparator implements Comparator {
    
    	@Override
    	public int compare(Object o1, Object o2) {
    		Employee e1=(Employee)o1;  
    		Employee e2=(Employee)o2;  
    		  
    		if(e1.empId==e2.empId)  
    		return 0;  
    		else if(e1.empId>e2.empId)  
    		return 1;  
    		else  
    		return -1;  
    	}
    
    }
    

    NameComparator:- This Class is used to sort the elements on the basis of Employee name.

    package com.comparator;
    
    import java.util.Comparator;
    
    public class NameCompartor implements Comparator {
    
    	@Override
    	public int compare(Object o1, Object o2) {
    		Employee e1=(Employee)o1;  
    		Employee e2=(Employee)o2;  
    		  
    		return e1.name.compareTo(e2.name);  
    	}
    
    }
    

    Main.java

    package com.comparator;
    
    import java.util.*;  
    import java.io.*;  
      
    class Main{  
    	
    	public static void main(String args[]){  
      
    		Iterator itr=null;
    		ArrayList al=new ArrayList();  
    		al.add(new Employee(101,"Manish"));  
    		al.add(new Employee(106,"Akki"));  
    		al.add(new Employee(105,"Pratibha"));  
      
    		/*Sort the Elements on the basis of empId*/
    		Collections.sort(al, new EmpIdComparator());
    		itr=al.iterator();  
    		System.out.println("Sorting by Employee Id:-");
    		while(itr.hasNext()){  
    		Employee emp=(Employee)itr.next();  
    		System.out.println(emp.empId+" "+emp.name);  
    		}  
    		
    		/*Sort the Elements on the basis name*/
    		Collections.sort(al,new NameCompartor());
    		itr=al.iterator();  
    		System.out.println("\nSorting by Employee Name:-");
    		while(itr.hasNext()){  
    		Employee emp=(Employee)itr.next();  
    		System.out.println(emp.empId+" "+emp.name);  
    		}  
    	}  
    }  

    In the above example we have sort the employees by their name's and id's.

 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: