Comparator interface in java

Comparator interface is used to order the objects of user-defined class. This interface is found in java.util package and contains 2 methods compare(Object obj1,Object obj2) and equals(Object element).

Comparator Interface- Comparator interface provide following 2 methods- public int compare(Object o1, Object o2) public boolean equals(Object o)

interface Comparator
{
public int compare(Object o1, Object o2)// used to define order
public boolean equals(Object o)// used to define equality
}

Syntax of compare method

public int compare(Object obj1,Object obj2): compares the first object with second object.

Difference-between-comparable-and-comparator

Comparable and Comparator both are interfaces.

ecxeption1

But there are many differences between Comparable and Comparator interfaces that are given below.

SN Comparable Comparator
1 Comparable gives single sorting arrangement. At the end of the day, we can sort the collection on the premise of single component, for example, id or name or cost and so on. Comparator gives various sorting arrangement. At the end of the day, we can sort the accumulation on the premise of numerous components, for example, id, name and cost and so forth.
2 Comparable affects the original class i.e. actual class is modified. Comparator doesn't affect the original class i.e. actual class is not modified.
3 Comparable provides compareTo() method to sort elements. Comparator provides compare(), method to sort elements
4 Comparable is found in java.lang package. Comparator is found in java.util package.
5 We can sort the list elements of Comparable type by Collections.sort(List) method We can sort the list elements of Comparator type by Collections.sort(List,Comparator) method.

Example of Comparator

Example 1 - (Real Senario)
class Emp implements Comparable
{
	String name;
	String job;
	int salary;
	public Emp(String n,String j,int s)
	{

	this.name=n;
	this.job=j;
	this.salary=s;	
	}
	public void display()
	{
		System.out.println(name+"\t"+job+"\t"+salary);
	}
	public int compareTo(Object o)
	{
	Emp e1=(Emp)o;
	return this.name.compareTo(e1.name);
   //return this.job.compareTo(e1.job);
	//return this.salary-e1.salary;
}

}

import java.util.*;
class JobComparator implements Comparator
{
	public int compare(Object o, Object p)
	{
	Emp e1=(Emp)o;
	Emp e2=(Emp)p;
	return e1.job.compareTo(e2.job);
}

}

import java.util.*;
class SalaryComparator implements Comparator
{
public int compare(Object o, Object p)
{
	Emp e1=(Emp)o;
	Emp e2=(Emp)p;
	return e1.salary-e2.salary;
	}
}
import java.util.*;
class TreeSetDemo
{
	public static void main(String args[])
	{
		TreeSet tree=new TreeSet();
		// Comparable is use by default
		// Time of object adding by default Comparable is call
		tree.add(new Emp("zahid","trainer",45000));
		tree.add(new Emp("deepak","executive",15000));
		tree.add(new Emp("rajan","developer",55000));
	tree.add(new Emp("ashish","trainee",12000));
		System.out.println("Content of set in ascending order of name");
		Iterator itr=tree.iterator();
		while(itr.hasNext())
		{
			Emp e1=(Emp)itr.next();
			e1.display();
		}

		TreeSet tree1=new TreeSet(new JobComparator());
		// JobComparator shall be used
		tree1.addAll(tree);  // Time of object adding JobComparator is call
	System.out.println("Content of set in ascending order of job");
		itr=tree1.iterator();
		while(itr.hasNext())
		{
		Emp e1=(Emp)itr.next();
		e1.display();
	}
	Emp employee=new Emp("rajan","developer",55000);
	System.out.println("Searching Result is: "+tree1.contains(employee));
		TreeSet tree2=new TreeSet(new SalaryComparator());
		// SalaryComparator shall be used
		tree2.addAll(tree);  // Time of object adding SalaryComparator is call
		System.out.println("Content of set in ascending order of salary");
		itr=tree2.iterator();
		while(itr.hasNext())
		{
			Emp e1=(Emp)itr.next();
			e1.display();
		}
		Emp employee1=new Emp("rajan","developer",55000);
		tree2.remove(employee1);
		System.out.println("After Removing Numerb of object in TreeSet are : "+tree2.size());
	}
}

Output

Content of set in ascending order of name
ashish  trainee 12000
deepak  executive       15000
rajan   developer       55000
zahid   trainer 45000
Content of set in ascending order of job
rajan   developer       55000
deepak  executive       15000
ashish  trainee 12000
zahid   trainer 45000
Searching Result is: true
Content of set in ascending order of salary
ashish  trainee 12000
deepak  executive       15000
zahid   trainer 45000
rajan   developer       55000
After Removing Numerb of object in TreeSet are : 3