Sunday, 19 January 2014

Comparable vs Comparator

A java program using Comparable Interface  :

package org.pallav.sort;                                                                                                  
                                                                                                                          
public class Employee implements Comparable {                                                                             
                                                                                                                          
    /**                                                                                                                   
     * @author pallav rajput                                                                                              
     */                                                                                                                   
                                                                                                                          
    int empid;                                                                                                            
    String name;                                                                                                          
    int salary;                                                                                                           
                                                                                                                          
    public Employee(int empid, String name, int salary) {                                                                 
        this.empid = empid;                                                                                               
        this.name = name;                                                                                                 
        this.salary = salary;                                                                                             
                                                                                                                          
    }                                                                                                                     
                                                                                                                          
    @Override                                                                                                             
    public int compareTo(Object arg0) {
        Employee employee = (Employee) arg0; 
        return (this.empid < employee.empid) ? -1
        : (this.empid > employee.empid) ? 1 : 0;
                                                                                                                          
    }                                                                                                                     
                                                                                                                          
    public int getEmpid() {                                                                                               
        return empid;                                                                                                     
    }                                                                                                                     
                                                                                                                          
    public void setEmpid(int empid) {                                                                                     
        this.empid = empid;                                                                                               
    }                                                                                                                     
                                                                                                                          
    public String getName() {                                                                                             
        return name;                                                                                                      
    }                                                                                                                     
                                                                                                                          
    public void setName(String name) {                                                                                    
        this.name = name;                                                                                                 
    }                                                                                                                     
                                                                                                                          
    public int getSalary() {                                                                                              
        return salary;                                                                                                    
    }                                                                                                                     
                                                                                                                          
    public void setSalary(int salary) {                                                                                   
        this.salary = salary;                                                                                             
    }                                                                                                                     
                                                                                                                          
}                       



                                                                                                                                                                                                                                        
package org.pallav.sort;

import java.util.ArrayList;
import java.util.Collections;

public class ComparableMain {

    /**
     * @author Pallav Rajput
     */

    public static void main(String[] args) {

        Employee emp = new Employee(1, "Pallav", 44000);
        Employee emp1 = new Employee(7, "Pranay", 45000);
        Employee emp2 = new Employee(11, "Abhinay", 42000);
        Employee emp3 = new Employee(8, "Salty", 42000);
        Employee emp4 = new Employee(4, "Shikhar", 38000);

        ArrayList<Employee> listofEmployee = new ArrayList<Employee>();

        listofEmployee.add(emp);
        listofEmployee.add(emp1);
        listofEmployee.add(emp2);
        listofEmployee.add(emp3);
        listofEmployee.add(emp4);

        System.out.println("Before Sort  : ");

        for (int i = 0; i < listofEmployee.size(); i++) {
            Employee employee = (Employee) listofEmployee.get(i);
            System.out.println("Employee Id: " + employee.getEmpid() + "||"
                    + "Employee Name: " + employee.getName() + "||"
                    + "Employee Salary:" + employee.getSalary());
        }

        Collections.sort(listofEmployee);

        for (int i = 0; i < listofEmployee.size(); i++) {
            Employee employee = (Employee) listofEmployee.get(i);
            System.out.println("Employee Id: " + employee.getEmpid() + "||"
                    + "Employee Name: " + employee.getName() + "||"
                    + "Employee Salary:" + employee.getSalary());
        }

    }

}

Output :

Before Sort  :
Employee Id: 1 || Employee Name: Pallav || Employee Salary:44000
Employee Id: 7 || Employee Name: Pranay || Employee Salary:45000
Employee Id: 11 || Employee Name: Abhinay || Employee Salary:42000
Employee Id: 8 || Employee Name: Salty || Employee Salary:42000
Employee Id: 4 || Employee Name: Shikhar || Employee Salary:38000

After Sort  :
Employee Id: 1 || Employee Name: Pallav || Employee Salary:44000
Employee Id: 4 || Employee Name: Shikhar || Employee Salary:38000
Employee Id: 7 || Employee Name: Pranay || Employee Salary:45000
Employee Id: 8 || Employee Name: Salty || Employee Salary:42000
Employee Id: 11 || Employee Name: Abhinay || Employee Salary:42000


A java program using Comparator Interface  : 


package org.pallav.sort;                                                                                                  
                                                                                                                          
public class Employee  {                                                                             
                                                                                                                          
    /**                                                                                                                   
     * @author pallav rajput                                                                                              
     */                                                                                                                   
                                                                                                                          
    int empid;                                                                                                            
    String name;                                                                                                          
    int salary;                                                                                                           
                                                                                                                          
    public Employee(int empid, String name, int salary) {                                                                 
        this.empid = empid;                                                                                               
        this.name = name;                                                                                                 
        this.salary = salary;                                                                                             
                                                                                                                          
    }                                                                                                                     
                                                                                                                          
    public int getEmpid() {                                                                                               
        return empid;                                                                                                     
    }                                                                                                                     
                                                                                                                          
    public void setEmpid(int empid) {                                                                                     
        this.empid = empid;                                                                                               
    }                                                                                                                     
                                                                                                                          
    public String getName() {                                                                                             
        return name;                                                                                                      
    }                                                                                                                     
                                                                                                                          
    public void setName(String name) {                                                                                    
        this.name = name;                                                                                                 
    }                                                                                                                     
                                                                                                                          
    public int getSalary() {                                                                                              
        return salary;                                                                                                    
    }                                                                                                                     
                                                                                                                          
    public void setSalary(int salary) {                                                                                   
        this.salary = salary;                                                                                             
    }                                                                                                                     
                                                                                                                          
}                          

package org.pallav.sort;

import java.util.Comparator;

public class SortbyIdComparator implements Comparator<Employee> {

    public int compare(Employee emp1, Employee emp2) {
        // TODO Auto-generated method stub
        return (emp1.getEmpid() < emp2.getEmpid()) ? -1
                : (emp1.getEmpid() > emp2.getEmpid()) ? 1 : 0;
    }

}


package org.pallav.sort;   

import java.util.ArrayList;
import java.util.Collections;

public class ComparatorMain {

   
    /**
     * @author pallav rajput
     */

    public static void main(String[] args) {

        Employee emp = new Employee(1, "Pallav", 44000);
        Employee emp1 = new Employee(7, "Pranay", 45000);
        Employee emp2 = new Employee(11, "Abhinay", 42000);
        Employee emp3 = new Employee(8, "Salty", 42000);
        Employee emp4 = new Employee(4, "Shikhar", 38000);

        ArrayList<Employee> listofEmployee = new ArrayList<Employee>();

        listofEmployee.add(emp);
        listofEmployee.add(emp1);
        listofEmployee.add(emp2);
        listofEmployee.add(emp3);
        listofEmployee.add(emp4);

        System.out.println("Before Sort  : ");

        for (int i = 0; i < listofEmployee.size(); i++) {
            Employee employee = (Employee) listofEmployee.get(i);
            System.out.println("Employee Id: " + employee.getEmpid() + " || "
                    + "Employee Name: " + employee.getName() + " || "
                    + "Employee Salary:" + employee.getSalary());
        }

        Collections.sort(listofEmployee, new SortbyIdComparator());
       
        System.out.println("After Sort  : ");

        for (int i = 0; i < listofEmployee.size(); i++) {
            Employee employee = (Employee) listofEmployee.get(i);
            System.out.println("Employee Id: " + employee.getEmpid() + " || "
                    + "Employee Name: " + employee.getName() + " || "
                    + "Employee Salary:" + employee.getSalary());
        }

    }

}

Output :

Before Sort  :
Employee Id: 1 || Employee Name: Pallav || Employee Salary:44000
Employee Id: 7 || Employee Name: Pranay || Employee Salary:45000
Employee Id: 11 || Employee Name: Abhinay || Employee Salary:42000
Employee Id: 8 || Employee Name: Salty || Employee Salary:42000
Employee Id: 4 || Employee Name: Shikhar || Employee Salary:38000

After Sort  :
Employee Id: 1 || Employee Name: Pallav || Employee Salary:44000
Employee Id: 4 || Employee Name: Shikhar || Employee Salary:38000
Employee Id: 7 || Employee Name: Pranay || Employee Salary:45000
Employee Id: 8 || Employee Name: Salty || Employee Salary:42000
Employee Id: 11 || Employee Name: Abhinay || Employee Salary:42000

1 comment: