Comparable and Comparator continue…

N:B: If you have not gone through my previous post about comparator then once go through the link Comparable & Comparator: then it will be easy to understand this topic.

Comparator:

  • Java Comparator interfaceis used to order the objects of user-defined class.
  • It is basically used for custom sorting, that means you can sort employee objects by name by salary.
  • Comparator interface is in java.util package and its having one method compare() which takes two user defined objects as argument.

public int compare(Object obj1,Object obj2)

  • Compare() method compares first object data member with second object data member and its return type is “int”.
  • While shorting through Collections.sort() method we have to pass the Collection object as well as the Comparator object.
  • Let me discuss it though examples.

Employee.java:

package com.tcoj.model;

 

public class Employee {

int id;

public String name;

public double salary;

@Override

public String toString() {

 

return “[“+id+”,”+name+”,”+salary+”]”;

}

public Employee(int id, String name, double salary) {

super();

this.id = id;

this.name = name;

this.salary = salary;

}

}

  • Now Let me write two comparators one as NameComparator another SalaryComparator for two custom comparing.
  • The Idea here is to compare the Employee objects through different fields.

NameComparator.java:

import java.util.Comparator;

import com.tcoj.model.Employee;

 

public class NameComparator implements Comparator<Employee> {

 

@Override

public int compare(Employee emp1, Employee emp2) {

 

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

}

 

}

 

SalaryComarator.java:

import java.util.Comparator;

 

import com.tcoj.model.Employee;

 

public class SalaryComparator implements Comparator<Employee> {

 

@Override

public int compare(Employee emp1, Employee emp2) {

double d=emp1.salary-emp2.salary;

if(d>0)

return 1;

else if(d < 0)

return -1;

else

return 0;

}

}

 

  • Now Let me write the client class where I will add some Employee objects and add them to list.
  • Then after passing two different Comparator objects we will see the sorting.

Client.java:

package com.tcoj.clint;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

import java.util.List;

 

import com.tcoj.comparators.NameComparator;

import com.tcoj.comparators.SalaryComparator;

import com.tcoj.model.Employee;

 

public class Clint {

 

public static void main(String[] args) {

List<Employee> list=new ArrayList<Employee>();

Employee emp1=new Employee(102, “shaswot”, 11500.89);

Employee emp2=new Employee(100, “subham”, 12500.89);

Employee emp3=new Employee(107, “ashok”, 11500.99);

Employee emp4=new Employee(108, “rahul”, 13501.09);

 

list.add(emp1);

list.add(emp2);

list.add(emp3);

list.add(emp4);

Collections.sort(list,new NameComparator()); //passing namecomparator object

Iterator<Employee> itr=list.iterator();

System.out.println(“Sort by name=============================”);

while(itr.hasNext())

{

System.out.println(itr.next());

}

System.out.println(“Sort by Salary===========================”);

Collections.sort(list,new SalaryComparator()); //passing salarycomparator object

Iterator<Employee> itr1=list.iterator();

while(itr1.hasNext())

{

System.out.println(itr1.next());

}

}

}

 

Output:

Sort by name=============================

[107,ashok,11500.99]

[108,rahul,13501.09]

[102,shaswot,11500.89]

[100,subham,12500.89]

Sort by Salary===========================

[102,shaswot,11500.89]

[107,ashok,11500.99]

[100,subham,12500.89]

[108,rahul,13501.09]

 

 

  • Here you can see when I am passing NameComparator it is sorting by name and same in SalaryComparator also.

If You guys have any doubt on this topic then feel free to comment and ask questions.

Comparable & Comparator:

Comparable & Comparator:

  • Comparable and Comparator both are interfaces and are used to do sorting in Collection framework.
  • Comparable is used for single sorting where as Comparator is used or multiple sorting.
  • I will discuss comparable first and its uses with examples then after we will go for Comparator.

Comparable Interface in java:

  • It is used to order the objects of user defined classes in defined order.
  • Comparable interface is in java.lang package and its having one method compareTo(Object) which takes user defined object as argument.
  • It provides Single sorting only that means you can sort the elements on based on single data member only.
  • compareTo() method compares current object data member with the specified object. The return type of the method is int.
  • Based on the return value that is (+ve,0,-ve) collection will sort the objects.
  • Lets go for a sample program and i will explain step by step.
public class Employee implements Comparable<Employee>{

int id;

String name;

double salary;

 

@Override

public int compareTo(Employee emp) {

 

return this.id-emp.id;

}

 

}

 

  • Here I wrote a Employee class which implements Comparable interface and I overridden compareTo()
  • Here I compared on a data member called “id” and retrned difference of the two employee object ids.
  • Now i will write the clint class where i will insert employee objects into a collection type and will sort it.
package com.tcoj.clint;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

import java.util.List;

 

import com.tcoj.model.Employee;

 

public class Clint {

 

public static void main(String[] args) {

List<Employee> list=new ArrayList<Employee>();

Employee emp1=new Employee(102, “shaswot”, 11500.89);

Employee emp2=new Employee(100, “subham”, 12500.89);

Employee emp3=new Employee(107, “ashok”, 11500.99);

Employee emp4=new Employee(103, “rahul”, 13501.09);

 

list.add(emp1);

list.add(emp2);

list.add(emp3);

list.add(emp4);

Collections.sort(list);

Iterator<Employee> itr=list.iterator();

while(itr.hasNext())

{

System.out.println(itr.next());

}

}

 

}

 

  • Here I created one List and added 4 employee objects.
  • When i called Collections.sort() method it sorted according to the compareTo() method defined in Employee class.

OUTPUT:

 

[100,subham,12500.89]

[102,shaswot,11500.89]

[103,rahul,13501.09]

[107,ashok,11500.99]

It sorted according to id of Employee.You guys can try with name and salary.Let me know with comments if you guys having any doubt on this part.Next topic Comparators.