January 16, 2016

COMPARABLE AND COMPARATOR INTERFACE

  1. 1. Package: 
         a). Available in java.lang.Comparable
         b). Available in java.util.Comparator

          2. Calling Method
               a). Objects are stored on the basis of CompareTo method
               b). Objects will be stored on the basis of Compare method in Comparator.

           3. Sorting Logic
                a). Sorting Logic must be in the same class.
                b). Sorting Logic is in separate class. That is the reason why we can write different sorting                        based on different attributes of objects to be stored.

           4. Implementation:
                 a). If in a class some of the objects are to be sorted, then that class must implements this interface.
                 b). In this they do not need to take such precautions, instead some other class may implement this interface.

            5. Sorting Logic:
                 a). int compareTo(Object o1)
This method compares this object with o1 object and returns a integer. Its value has following meaning
1. positive – this object is greater than o1
2. zero – this object equals to o1
3. negative – this object is less than o1

                 b). int compare(Object o1,Object o2)
This method compares o1 and o2 objects and returns a integer. Its value has following meaning.
1. positive – o1 is greater than o2
2. zero –  o1 equals to o2
3. negative – o1 is less than o1

I would like to demonstrate the difference between these two with the help of an example.  While coding we should follow some java conventions. Some of the conventions are like,Suppose you have been asked to sort Employee on the basis of their Id, Salary or Name.

  1. 1. Giving proper Class name
  2. 2. Maintaining POJO Class
  3. 3. Separate Class to avoid complexity

 

Procedure to make:

  1. 1. First we have to write a Employee Class with Id, Name and Salary
  2. 2. We will write one main class where all the operations are going to be done.
  3. 3. We will have a separate class For Id, name and Salary, which all will be extending Comparator.

 

Employee.java
package com.technoblogpost;

public class Employee {
	int id;
	public String name;
	long salary;
	
	 Employee(int id, String name, long salary) {
		 this.id = id;
		 this.name = name;
		 this.salary = salary;
	 }
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", salary=" + salary
				+ "]";
	} 
}
Main_Class.java
package com.technoblogpost;

import java.util.ArrayList;
import java.util.Collections;
public class Main_Class {
	public static void main(String[] args) {
		Employee employees_1 = new Employee(29, "Aman", 47852);
		Employee employees_2 = new Employee(14, "Shekhar", 12453);
		Employee employees_3 = new Employee(42, "Chandan", 25874);
		Employee employees_4 = new Employee(80, "Jackie", 98563);
		Employee employees_5 = new Employee(64, "Dexter", 12589);
		ArrayList employees_List = new ArrayList();
		employees_List.add(employees_1);
		employees_List.add(employees_2);
		employees_List.add(employees_3);
		employees_List.add(employees_4);
		employees_List.add(employees_5);
		System.out				.println("===========================================================================================================================");
		System.out				.println("===========================================================================================================================");
		System.out.println(employees_List);
		System.out				.println("===========================================================================================================================");
		Collections.sort(employees_List, new By_Id());
		System.out				.println("===========================================================================================================================");
		System.out.println(employees_List);
		System.out				.println("===========================================================================================================================");
		Collections.sort(employees_List, new By_Name());
		System.out				.println("===========================================================================================================================");
		System.out.println(employees_List);
		System.out	.println("===========================================================================================================================");
		Collections.sort(employees_List, new By_Salary());
		System.out		.println("===========================================================================================================================");
		System.out.println(employees_List);
		System.out		.println("===========================================================================================================================");
		System.out			.println("===========================================================================================================================");
	}
}
By_Id.java
package com.technoblogpost;

import java.util.Comparator;

public class By_Id implements Comparator {

	@Override
	public int compare(Employee emp1, Employee emp2) {
		// TODO Auto-generated method stub
		return emp1.id - emp2.id;
	}

}
By_Name.java
package com.technoblogpost;

import java.util.Comparator;

public class By_Name implements Comparator {

	@Override
	public int compare(Employee emp1, Employee emp2) {
		// TODO Auto-generated method stub
		return emp1.name.compareTo(emp2.name);
	}
}
By_Salary.java
package com.technoblogpost;

import java.util.Comparator;

public class By_Salary implements Comparator {

	@Override
	public int compare(Employee emp1, Employee emp2) {
		// TODO Auto-generated method stub
		return (int) (emp1.salary - emp2.salary);
	}
}

In this program you have seen that, we are using Comparator. The same program I’m going to demonstrate with the help of Comparable Interface.

Employee.java
package com.technoblogpost;

public class Employee implements Comparable {
	int id;
	String name;
	long salary;
	
	Employee(int id, String name, long salary) {
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
	
	@Override
	public String toString() {
		return "Employee [id = " + id + ", name = " + name + ", salary = " + salary
				+ "]";
	}
	
	@Override
	public int compareTo(Employee emp) {
		return id - emp.id;
	}
}
 
Main_Class.java

package com.technoblogpost;
import java.util.ArrayList;
import java.util.Collections;

public class Main_Class {
	public static void main(String[] args) {
		Employee employees1 = new Employee(40, "Aman", 75000);
		Employee employees2 = new Employee(20, "Jackie", 71000);
		Employee employees3 = new Employee(10, "Shekhar", 45000);
		Employee employees4 = new Employee(50, "Schofield", 65000);
		Employee employees5 = new Employee(70, "Chandaan", 79000);
		
		ArrayList employees_list = new ArrayList<>();
		employees_list.add(employees1);
		employees_list.add(employees2);
		employees_list.add(employees3);
		employees_list.add(employees4);
		employees_list.add(employees5);
		System.out.println(employees_list);
		Collections.sort(employees_list);
		System.out.println(employees_list);
	}
}

The above program is by using Comparable Interface.