View RSS Feed

My Java Tips

Difference Between Comparable And Comparator Interface

Rating: 1 votes, 4.00 average.
by , 11-08-2011 at 06:29 PM (7876 Views)
Programmers are usually confused between "Comparable" and "Comparator" interface. Comparable interface has "compareTo" method which is normally used for natural ordering but Comparator interface has "compare" method which takes two arguments. It can be used where you want to sort objects based of more then one parameter. Following example will make it more clear.




Java Code:
package test;/*
 ** Use the Collections.sort to sort a List
 **
 ** When you need natural sort order you can implement
 ** the Comparable interface.
 **
 ** If You want an alternate sort order or sorting on different properties
 * then implement a Comparator for your class.
 */
import java.util.*;

public class Farmer implements Comparable
{
	String name;
	int age;
	long income;

	public Farmer(String name, int age)
	{
		this.name = name;
		this.age = age;
	}

	public Farmer(String name, int age,long income)
	{
		this.name = name;
		this.age = age;
		this.income=income;
	}
	public String getName()
	{
		return name;
	}

	public int getAge()
	{
		return age;
	}

	public String toString()
	{
		return name + " : " + age;
	}

	/*
	 ** Implement the natural order for this class
	 */
	public int compareTo(Object o)
	{
		return getName().compareTo(((Farmer)o).getName());
	}

	static class AgeComparator implements Comparator
	{

		/*
		 * (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 *
		 */
		public int compare(Object o1, Object o2)
		{
			Farmer p1 = (Farmer)o1;
			Farmer p2 = (Farmer)o2;
			if(p1.getIncome()==0 && p2.getIncome()==0 )
				return p1.getAge() - p2.getAge();
			else
				return (int)(p1.getIncome() -p2.getIncome());
		}
	}

	public static void main(String[] args)
	{
		List farmer = new ArrayList();
		farmer.add( new Farmer("Joe", 34) );
		farmer.add( new Farmer("Ali", 13) );
		farmer.add( new Farmer("Mark", 25) );
		farmer.add( new Farmer("Dana", 66) );

		Collections.sort(farmer);
		System.out.println("Sort in Natural order");
		System.out.println("t" + farmer);

		Collections.sort(farmer, Collections.reverseOrder());
		System.out.println("Sort by reverse natural order");
		System.out.println("t" + farmer);

		List farmerIncome = new ArrayList();
		farmerIncome.add( new Farmer("Joe", 34,33));
		farmerIncome.add( new Farmer("Ali", 13,3));
		farmerIncome.add( new Farmer("Mark", 25,666));
		farmerIncome.add( new Farmer("Dana", 66,2));

		Collections.sort(farmer, new AgeComparator());
		System.out.println("Sort using Age Comparator");
		System.out.println("t" + farmer);

		Collections.sort(farmerIncome, new AgeComparator());
		System.out.println("Sort using Age Comparator But Income Wise");
		System.out.println("t" + farmerIncome);

	}

	public long getIncome() {
		return income;
	}

	public void setIncome(long income) {
		this.income = income;
	}

	public void setAge(int age) {
		this.age = age;
	}

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

Sort in Natural order
[Ali : 13, Dana : 66, Joe : 34, Mark : 25]
Sort by reverse natural order
[Mark : 25, Joe : 34, Dana : 66, Ali : 13]
Sort using Age Comparator
[Ali : 13, Mark : 25, Joe : 34, Dana : 66]
Sort using Age Comparator But Income Wise
[Joe : 34, Ali : 13, Mark : 25, Dana : 66]

Submit "Difference Between Comparable And Comparator Interface" to Facebook Submit "Difference Between Comparable And Comparator Interface" to Digg Submit "Difference Between Comparable And Comparator Interface" to del.icio.us Submit "Difference Between Comparable And Comparator Interface" to StumbleUpon Submit "Difference Between Comparable And Comparator Interface" to Google

Comments