Monday, 12 March 2018

Comparator in JAVA

Java.util.Comparator is an interface in java to order/sorting the elements of a user defined class. This has 2 methods

1. public int compare(Object o1, Object o2) -- Compare it's two arguments for order and return a negative integer,  zero or positive integer  as first argument is less than or equal to or greater than second element.
2. public equals(Object o) --- Indicated whether some other object is equal to this comparator.


package com.sample.java.testing;

import java.util.Comparator;

public class ComparatorNameTest implements Comparator<EmployeeClass> {

    //compare and sort baed on emp name
    @Override
    public int compare(EmployeeClass o1, EmployeeClass o2) {
        return o1.getName().compareTo(o2.getName());
    }

}


package com.sample.java.testing;

import java.util.Comparator;

public class ComparatorIdTest implements Comparator<EmployeeClass> {

    @Override
    public int compare(EmployeeClass o1, EmployeeClass o2) {
       return o1.getId() - o2.getId();
    }

}


package com.sample.java.testing;

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

public class EmployeeClass {

    private int id;
    private String name;
    private double salary;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
   
    public String toString() {
        return "Id of employee is " + this.id + " name of emloyess "  + this.name + " salary is " + this.salary;
    }

    public EmployeeClass(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

}

package com.sample.java.testing;

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

public class ComparatorDemoTest {

    public static void main(String a[]) {
        EmployeeClass employeeClass = new EmployeeClass(60, "Java", 345678);
        EmployeeClass employeeClass1 = new EmployeeClass(50, "J2ee", 9876543);
        EmployeeClass employeeClass2 = new EmployeeClass(30, "Spring", 12345);
        EmployeeClass employeeClass3 = new EmployeeClass(40, "Jdbc", 654);

        List<EmployeeClass> employess = new ArrayList<>();
        employess.add(employeeClass3);
        employess.add(employeeClass2);
        employess.add(employeeClass1);
        employess.add(employeeClass);
        System.out.println("before comarator sorting....");
        for (EmployeeClass employeeClass4 : employess) {
            System.out.println(employeeClass4.toString());// no order it will print emp details randomly
        }
       
        System.out.println("after comarator sorting using emp name....");
        Collections.sort(employess, new ComparatorNameTest());
       
        for (EmployeeClass employeeClass4 : employess) {
            System.out.println(employeeClass4.toString());//  order by emp name it will print emp details based  on name asc order
        }
       
        System.out.println("after comarator sorting using emp id....");
        Collections.sort(employess, new ComparatorIdTest());
       
        for (EmployeeClass employeeClass4 : employess) {
            System.out.println(employeeClass4.toString());//  order by emp name it will print emp details based on id asc order
        }
    }

}



Output:

before comarator sorting....
Id of employee is 40 name of emloyess Jdbc salary is 654.0
Id of employee is 30 name of emloyess Spring salary is 12345.0
Id of employee is 50 name of emloyess J2ee salary is 9876543.0
Id of employee is 60 name of emloyess Java salary is 345678.0
after comarator sorting using emp name....
Id of employee is 50 name of emloyess J2ee salary is 9876543.0
Id of employee is 60 name of emloyess Java salary is 345678.0
Id of employee is 40 name of emloyess Jdbc salary is 654.0
Id of employee is 30 name of emloyess Spring salary is 12345.0
after comarator sorting using emp id....
Id of employee is 30 name of emloyess Spring salary is 12345.0
Id of employee is 40 name of emloyess Jdbc salary is 654.0
Id of employee is 50 name of emloyess J2ee salary is 9876543.0
Id of employee is 60 name of emloyess Java salary is 345678.0


We can sort collections in different ways based on parameter which we want using comparator interface.

No comments:

Post a Comment