Core Java

Difference between Comparator and Comparable in Java

One of the common interview question is ‘What are differences between Comparator and Comparable’. or ‘How will you sort collection of employee objects by its id or name’.For that we can use two interfaces.i.e. Comparator and Comparable.Before we actually see differences,let me give you brief introduction of both.

Comparable interface: Class whose objects to be sorted must implement this interface.In this,we have to implement compareTo(Object) method.

For example:

public class Country implements Comparable{
       @Override
    public int compareTo(Object arg0) {
        Country country=(Country) arg0;
        return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ;
}}

If any class implements comparable inteface then collection of that object can be sorted automatically using Collection.sort() or Arrays.sort().Object will be sort on the basis of compareTo method in that class.

Objects which implement Comparable in java can be used as keys in a SortedMap like TreeMap or SortedSet like TreeSet without implementing any other interface.

Comparator interface: Class whose objects to be sorted do not need to implement this interface.Some third class can implement this interface to sort.e.g.CountrySortByIdComparator class can implement Comparator interface to sort collection of country object by id. For example:

public class CountrySortByIdComparator implements Comparator<Country>{

    @Override
    public int compare(Country country1, Country country2) {

        return (country1.getCountryId() < country2.getCountryId() ) ? -1: (country1.getCountryId() > country2.getCountryId() ) ? 1:0 ;
    }

}

Using Comparator interface,we can write different sorting based on different attributes of objects to be sorted.You can use anonymous comparator to compare at particular line of code. For example:

         Country indiaCountry=new Country(1, 'India');
         Country chinaCountry=new Country(4, 'China');
         Country nepalCountry=new Country(3, 'Nepal');
         Country bhutanCountry=new Country(2, 'Bhutan');

         List<Country> listOfCountries = new ArrayList<Country>();
         listOfCountries.add(indiaCountry);
         listOfCountries.add(chinaCountry);
         listOfCountries.add(nepalCountry);
         listOfCountries.add(bhutanCountry); 

 //Sort by countryName

            Collections.sort(listOfCountries,new Comparator<Country>() {

                @Override
                public int compare(Country o1, Country o2) {

                    return o1.getCountryName().compareTo(o2.getCountryName());
                }
            });

Comparator vs Comparable

ParameterComparableComparator
Sorting logicSorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objectsSorting logic is in separate class. Hence we can write different sorting based on different attributes of objects to be sorted. E.g. Sorting using id,name etc.
ImplementationClass whose objects to be sorted must implement this interface.e.g Country class needs to implement comparable to collection of country object by idClass whose objects to be sorted do not need to implement this interface.Some other class can implement this interface. E.g.-CountrySortByIdComparator class can implement Comparator interface to sort collection of country object by id

Sorting method
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
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
Calling methodCollections.sort(List)
Here objects will be sorted on the basis of CompareTo method
Collections.sort(List, Comparator)
Here objects will be sorted on the basis of Compare method in Comparator
PackageJava.lang.Comparable

 

Java.util.Comparator

Java code:
For Comparable: We will create class country having attribute id and name.This class will implement Comparable interface and implement CompareTo method to sort collection of country object by id.

1. Country.java

package org.arpit.javapostsforlearning;
//If this.cuntryId < country.countryId:then compare method will return -1
//If this.countryId > country.countryId:then compare method will return 1
//If this.countryId==country.countryId:then compare method will return 0
public class Country implements Comparable{
    int countryId;
    String countryName;

    public Country(int countryId, String countryName) {
        super();
        this.countryId = countryId;
        this.countryName = countryName;
    }

    @Override
    public int compareTo(Object arg0) {
        Country country=(Country) arg0;
        return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ;
    }

    public int getCountryId() {
        return countryId;
    }

    public void setCountryId(int countryId) {
        this.countryId = countryId;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

}

2.ComparatorMain.java

package org.arpit.javapostsforlearning;

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

public class ComparatorMain {

	/**
	 * @author Arpit Mandliya
	 */
	public static void main(String[] args) {
		 Country indiaCountry=new Country(1, 'India');
		 Country chinaCountry=new Country(4, 'China');
		 Country nepalCountry=new Country(3, 'Nepal');
		 Country bhutanCountry=new Country(2, 'Bhutan');

	        List<Country> listOfCountries = new ArrayList<Country>();
	        listOfCountries.add(indiaCountry);
	        listOfCountries.add(chinaCountry);
	        listOfCountries.add(nepalCountry);
	        listOfCountries.add(bhutanCountry);

	        System.out.println('Before Sort  : ');
	        for (int i = 0; i < listOfCountries.size(); i++) {
				Country country=(Country) listOfCountries.get(i);
				System.out.println('Country Id: '+country.getCountryId()+'||'+'Country name: '+country.getCountryName());
			}
	        Collections.sort(listOfCountries);

	        System.out.println('After Sort  : ');
	        for (int i = 0; i < listOfCountries.size(); i++) {
				Country country=(Country) listOfCountries.get(i);
				System.out.println('Country Id: '+country.getCountryId()+'|| '+'Country name: '+country.getCountryName());
			}
	}

}

Output:

Before Sort  : 
Country Id: 1||Country name: India
Country Id: 4||Country name: China
Country Id: 3||Country name: Nepal
Country Id: 2||Country name: Bhutan
After Sort  : 
Country Id: 1|| Country name: India
Country Id: 2|| Country name: Bhutan
Country Id: 3|| Country name: Nepal
Country Id: 4|| Country name: China

For Comparator: We will create class country having attribute id and name and will create another class CountrySortByIdComparator which will implement Comparator interface and implement compare method to sort collection of country object by id and we will also see how to use anonymous comparator.

1.Country.java

package org.arpit.javapostsforlearning;

public class Country{
    int countryId;
    String countryName;

    public Country(int countryId, String countryName) {
        super();
        this.countryId = countryId;
        this.countryName = countryName;
    }

    public int getCountryId() {
        return countryId;
    }

    public void setCountryId(int countryId) {
        this.countryId = countryId;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

}

2.CountrySortbyIdComparator.java

package org.arpit.javapostsforlearning;

import java.util.Comparator;
//If country1.getCountryId()<country2.getCountryId():then compare method will return -1
//If country1.getCountryId()>country2.getCountryId():then compare method will return 1
//If country1.getCountryId()==country2.getCountryId():then compare method will return 0
 public class CountrySortByIdComparator implements Comparator<Country>{

    @Override
    public int compare(Country country1, Country country2) {

        return (country1.getCountryId() < country2.getCountryId() ) ? -1: (country1.getCountryId() > country2.getCountryId() ) ? 1:0 ;
    }

}

3.ComparatorMain.java

package org.arpit.javapostsforlearning;

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

public class ComparatorMain {

	/**
	 * @author Arpit Mandliya
	 */
	public static void main(String[] args) {
		 Country indiaCountry=new Country(1, 'India');
		 Country chinaCountry=new Country(4, 'China');
		 Country nepalCountry=new Country(3, 'Nepal');
		 Country bhutanCountry=new Country(2, 'Bhutan');

	        List<Country> listOfCountries = new ArrayList<Country>();
	        listOfCountries.add(indiaCountry);
	        listOfCountries.add(chinaCountry);
	        listOfCountries.add(nepalCountry);
	        listOfCountries.add(bhutanCountry);

	        System.out.println('Before Sort by id : ');
	        for (int i = 0; i < listOfCountries.size(); i++) {
				Country country=(Country) listOfCountries.get(i);
				System.out.println('Country Id: '+country.getCountryId()+'||'+'Country name: '+country.getCountryName());
			}
	        Collections.sort(listOfCountries,new CountrySortByIdComparator());

	        System.out.println('After Sort by id: ');
	        for (int i = 0; i < listOfCountries.size(); i++) {
				Country country=(Country) listOfCountries.get(i);
				System.out.println('Country Id: '+country.getCountryId()+'|| '+'Country name: '+country.getCountryName());
			}

	        //Sort by countryName
	        Collections.sort(listOfCountries,new Comparator<Country>() {

				@Override
				public int compare(Country o1, Country o2) {
					return o1.getCountryName().compareTo(o2.getCountryName());
				}
			});

			System.out.println('After Sort by name: ');
	        for (int i = 0; i < listOfCountries.size(); i++) {
				Country country=(Country) listOfCountries.get(i);
				System.out.println('Country Id: '+country.getCountryId()+'|| '+'Country name: '+country.getCountryName());
			}
	}

}

Output:

Before Sort by id : 
Country Id: 1||Country name: India
Country Id: 4||Country name: China
Country Id: 3||Country name: Nepal
Country Id: 2||Country name: Bhutan
After Sort by id: 
Country Id: 1|| Country name: India
Country Id: 2|| Country name: Bhutan
Country Id: 3|| Country name: Nepal
Country Id: 4|| Country name: China
After Sort by name: 
Country Id: 2|| Country name: Bhutan
Country Id: 4|| Country name: China
Country Id: 1|| Country name: India
Country Id: 3|| Country name: Nepal

 

Reference: Difference between Comparator and Comparable in Java from our JCG partner Arpit Mandliya at the Java frameworks and design patterns for beginners blog.

Arpit Mandliya

I am java developer at Tata Consultancy Services Ltd. My current area of interest are J2EE,web development and java design patterns. I am technology enthusiast trying to explore new technologies. In spare time,I love blogging.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

41 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Vipul Shukla
Vipul Shukla
11 years ago

Hi Arpit,

I have a doubt here. even in case of comparable, we can sort the object based on its attributes…like countryId in this case. Does this mean that the main difference is that we can only choose to sort using one attribute in case of comparable, however in case of comparator we can provide various implementations of compare method??

Jai
Jai
11 years ago
Reply to  Vipul Shukla

Yes, but in different classes. like below.

///Sort by ID
public class CountrySortByIdComparator implements Comparator{

public int compare(Country country1, Country country2) {
// your logic
}

//Sort By Name
public class CountrySortByNameComparator implements Comparator{

public int compare(Country country1, Country country2) {
// your logic
}

//Sort By Designation
public class CountrySortByDesignationComparator implements Comparator{

public int compare(Country country1, Country country2) {
// your logic
}

///// Some where in other class’s Main

Collections.sort(listOfCountries,new CountrySortByIdComparator());

Collections.sort(listOfCountries,new CountrySortByNameComparator());

Collections.sort(listOfCountries,new CountrySortByDesignationComparator());

Sharif
9 years ago
Reply to  Jai

hi,

As you are saying that you need different class to sort by different attributes. Can I not sort the list on multiple attributes in the same class?

Thanks
Sharif

Farhan Haque
Farhan Haque
9 years ago
Reply to  Sharif

Well, NO. This is because one class can have only a single implementation of the Comparator interface. You can’t overload the “compare(obj1,obj2)” method in a given class because the function signature of the compare method cannot be altered otherwise it won’t be invoked at all!
So for every implementation you need to have a different class that defines the compare method in the desired way.

Hope this clears your query!

Jai
Jai
9 years ago
Reply to  Farhan Haque

Two countries can not same name so in this case, yes, it will be sorted by name only. Lets have a new class Employee. Two Employees can have same name. Please Try with that . You will see, if two employees have same name then they will be sorted by their IDs.

Farhan Haque
Farhan Haque
9 years ago
Reply to  Farhan Haque

It still gives first priority to Names. Mate, you fail to understand that your code sorts by name and goes for the ID ONLY IF the names are same. Refer to the country example. Your code would output the following:-

2 Bhutan
4 China
1 India
3 Nepal

This is sorting by name. The ID sort would render the following:-

1 India
2 Bhutan
3 Nepal
4 China

So for implementing the ID sort you will be required to have a different class. Hope this clears your confusion.

Cheers!

Jai
Jai
9 years ago
Reply to  Farhan Haque

Hello Farhan, I never said we can achieve full sorting based on two different parameters together. Neither my code will do that. It will sort based on ID when the Names are same. Saw your FB profile noticed you are still in college. So I think you never attended any job interview. Its a very good interview question that how to sort a collection of a Class based on two parameter i.e Name or ID. Employee class is very common example for this. Two Employee can have same name but not ID. Replying in this thread because not seeing “reply”… Read more »

Jai
Jai
9 years ago
Reply to  Sharif

As you are saying that you need different class to sort by different attributes? No. It depends on requirement.
Can I not sort the list on multiple attributes in the same class? Yes.

How?

Eg. Lets take Country class from above and make one comparator of it.

//This Comparator will the Country Collection based on Name and Id both
public class CountrySortByNameNId implements Comparator{

public int compare(Country c1, Country c2) {
int i = c1.getCountryName().compareTo(c2.getCountryName());

if(i==0)
i = c1.getCountryId() – c2.getCountryId();

return i;
}

Farhan Haque
Farhan Haque
9 years ago
Reply to  Jai

Jai, you seem to have not understood the concept of sorting by Name and sorting by ID. The two are independent of each other. Check your code for a case where the each country has a different name and a different ID. You will end up sorting them on the basis of their names and not IDs. What you have written is a special case which sorts by ID only if the country names are same, otherwise the sorting implementation relies on the name aspect only. It does not pay heed to the ID at all if all the countries… Read more »

Farhan Haque
Farhan Haque
9 years ago
Reply to  Jai

Jai, you seem to have not understood the notion of sorting by name and sorting by ID. The two are indeed separate implementations of the Comparator interface and are independent of each other. Check your code for a case wherein all the countries have different names with unique IDs. You will gradually end up sorting them in the order of their names and not IDs. What you have specified is NOT sorting the country objects by IDs, perhaps it is sorting them by name and if and only if the names of two countries are same(which is practically impossible), it… Read more »

Laksman
Laksman
9 years ago
Reply to  Sharif

We can sort the list on different properties

Collections.sort(countriesList, new Comparator()
{
public int compare(final Country country1, final Country country2)
{
return new CompareToBuilder().append(country1.getCountryName(), country2.getCountryName())
.append(country1.getCountryId(), country2.getCountryId()).toComparison();
}
});

First it sorts on country name, if any country having the same name next priority is country Id.

Farhan
Farhan
9 years ago
Reply to  Jai

Jai, That’s exactly what I have been saying since the last so many comments – We can’t achieve full sorting based on two different parameters together. You finally got the point. Your code is plain and simple and does not do any massive task. I completely understand your code’s logic. It’s quite trivial. My being in college and not having appeared for any interview is immaterial of what I perceive of this question. I know quite a lot of people who have appeared for several interviews and yet are unable to produce a simple trivial code that many college going… Read more »

Prem Kumar
11 years ago

Comparator is easy to understand but sometimes a bit confusing because of the difficult to grab documentation from oracle or earlier Sun. It is one of the most important things you need to know while working with java.

Try reading it here http://javahash.com/java-comparator-and-comparable-demystified/

pops
pops
10 years ago

very good explaination

Yogesh
Yogesh
10 years ago

Excellent!!!

Anonymous
Anonymous
9 years ago
Reply to  Yogesh

Yes.

ashish
ashish
10 years ago

hey dude ,
Please remove the line numbers from the above code as those line no are also getting copied while copying the code

RaHuL A
10 years ago
Reply to  ashish

Hi Ashish,

You can use either of the “view source” or “copy to clipboard” buttons in upper right corner of the code snippet.. :)

Dilip Senapati
Dilip Senapati
10 years ago

Very good explanation, will be very beneficial for java learners.

Girish
Girish
10 years ago

Very good explanation …..Thanks to all……

nani
nani
10 years ago

please change the name of the class to ComparableMain instead of comparatorMain in the first sort program. That created a lot of disturbance for me to understand

vysh
vysh
10 years ago

very good and detailed article

Aamir
Aamir
10 years ago

Very good explanation, but I have a question. As everyone keep saying that compareTo method returns positive, negative etc values. I am just curious about who needs those values , I mean we simply call a sort method and the class whose objects we wanna sort implements comparable interface and we simply say in compareTo() method if one objects attrib is less or whatever than other then return something, ok if i have 10 objects in my list and in compareTo() we are saying the same thing as said above, then are first two objects in list first compared or… Read more »

anitha
anitha
10 years ago
Reply to  Aamir

I think if there are more objects to compare in the list, Collections.sort(list) would call compareTo method implemented in the (comparable) class, 2 objects at a time and sort it accordingly. For example: If ID is in this order on the list when added: 2,1,4,3. 2 and 1 compared first, the result would be 1,2 (if the implementation of compareTo is natural ordering and it would return positive number) 2 and 4 compared second, the result would be 2,4 (compareTo would return negative number) 4 and 3 compared third, the result would be 3,4 (compareTo would return negative number) End… Read more »

sam
sam
9 years ago
Reply to  anitha

Country indiaCountry=new Country(1, “India”);
Country chinaCountry=new Country(4, “China”);
Country nepalCountry=new Country(3, “Nepal”);
Country bhutanCountry=new Country(4, “Bhutan”);

Collections.sort(listOfCountries,new Comparator() {

@Override
public int compare(Country o1, Country o2) {

int result = (o1.getCountryId() o2.getCountryId() ) ? 1:0 ;
if(result==0 ){
result = o1.getCountryName().compareTo(o2.getCountryName());
}
return result;
}
});

Srinivasan
Srinivasan
10 years ago

Hi all, I have written sample program which implements comparable where you need not to explicitly put comparison logic inside compareTo method as below. return (this.countryId country.countryId ) ? 1:0 ; Please see my program for the difference, ################################################################################## package com.cisco.srini; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; class CompareProgram implements Comparable{ private final int petId; private final String petType; public CompareProgram(int petId, String petType) { this.petId = petId; this.petType = petType; } public int compareTo(Object o) { CompareProgram cmpObj = (CompareProgram)o; SOP(“Current PetType:”+this.petType); SOP(“Object to be compared:”+cmpObj.petType); int result = this.petType.compareTo(cmpObj.petType); SOP(“Compare value:”+result); return result; } public String… Read more »

Rudy Vissers
Rudy Vissers
10 years ago

Hi, Thanks for the tutorial. 1) If you make use of generics then please do not cast to the generic type ! Country country=(Country) listOfCountries.get(i); must become Country country=listOfCountries.get(i); It is not necessary anymore as you will use a foreach loop. 2) Do you know the foreach looping ? for (int i = 0; i < listOfCountries.size(); i++) { Country country = (Country) listOfCountries.get(i); System.out.println("Country Id: " + country.getCountryId() + "||" + "Country name: " + country.getCountryName()); } must become for (Country country : listOfCountries) { System.out.println("Country Id: " + country.getCountryId() + "|| " + "Country name: " + country.getCountryName());… Read more »

sam
sam
9 years ago
Reply to  Rudy Vissers

Perfect

nagappa
nagappa
10 years ago

Sorting logic is in separate class. Hence we can write different sorting based on different attributes of objects to be sorted.Is this mean applying differant sorting methods like bubble,quick etc sorts ?

suresh atta
10 years ago

This is very clear. Nice presentation.

suresh atta
10 years ago

And want to add one point with respect to use case. When we have control in Class to do implement we can use directly comparable. Otherwise, if it is a third party class, then we need to use Comparator for our own purpose.

Nitinp
Nitinp
9 years ago

Hi Arpit,

Blog is very nice, it helps me too much for the differences in these two. I think, just there is one minor print mistake while comparing in Comparator section i.e. “3. negative – o1 is less than o1” in third point

Thanks..!!!

PrasadS
PrasadS
9 years ago
Reply to  Nitinp

Yes Nitin , you have raised good point , it may lead confusion for novice

Shripad
Shripad
9 years ago

Thanks for you post.Very well description.

Jinsy
Jinsy
9 years ago

Thanks for the post. so helpful for understanding.

Janardhan
Janardhan
9 years ago

Nice work!

Magani
Magani
9 years ago

Nice explanation

Back to top button