Table of Contents
Comparable interface:
Class whose objects to be sorted must implement this interface.In this,we have to implement compareTo(Object) method.
For example:
1 2 3 4 5 6 7 |
public class Country implements Comparable{      @Override    public int compareTo(Country country) {       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:
1 2 3 4 5 6 7 8 9 10 11 |
public class CountrySortByIdComparator implements Comparator{    @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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
        Country indiaCountry=new Country(1, "India");        Country russiaCountry=new Country(4, "Russia");        Country englandCountry=new Country(3, "England");        Country germanyCountry=new Country(2, "Germany");                    List listOfCountries = new ArrayList();        listOfCountries.add(indiaCountry);        listOfCountries.add(russiaCountry);        listOfCountries.add(englandCountry);        listOfCountries.add(germanyCountry);  //Sort by countryName           Collections.sort(listOfCountries,new Comparator() {          @Override             public int compare(Country o1, Country o2) {                return o1.getCountryName().compareTo(o2.getCountryName());             }          }); |
Comparator vs Comparable
Parameter
|
Comparable
|
Comparator
|
---|---|---|
Sorting logic
|
Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects
|
Sorting 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.
|
Implementation
|
Class whose objects to be sorted must implement this interface.e.g Country class needs to implement comparable to collection of country object by id |
Class 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
|
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 method
|
Collections.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 |
Package
|
Java.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
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(Country country) {      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.ComparableMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package org.arpit.javapostsforlearning; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ComparableMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { Country indiaCountry=new Country(1, "India"); Country russiaCountry=new Country(4, "Russia"); Country englandCountry=new Country(3, "England"); Country germanyCountry=new Country(2, "Germany"); List listOfCountries = new ArrayList(); listOfCountries.add(indiaCountry); listOfCountries.add(russiaCountry); listOfCountries.add(englandCountry); listOfCountries.add(germanyCountry); 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()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
Before Sort : Country Id: 1||Country name: India Country Id: 4||Country name: Russia Country Id: 3||Country name: England Country Id: 2||Country name: Germany After Sort : Country Id: 1|| Country name: India Country Id: 2|| Country name: Germany Country Id: 3|| Country name: England Country Id: 4|| Country name: Russia |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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{    @Override    public int compare(Country country1, Country country2) {             return (country1.getCountryId() < country2.getCountryId() ) ? -1: (country1.getCountryId() > country2.getCountryId() ) ? 1:0 ;    } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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 russiaCountry=new Country(4, "Russia"); Country englandCountry=new Country(3, "England"); Country germanyCountry=new Country(2, "Germany"); List listOfCountries = new ArrayList(); listOfCountries.add(indiaCountry); listOfCountries.add(russiaCountry); listOfCountries.add(englandCountry); listOfCountries.add(germanyCountry); 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() { @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()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Before Sort by id : Country Id: 1||Country name: India Country Id: 4||Country name: Russia Country Id: 3||Country name: England Country Id: 2||Country name: Germany After Sort by id: Country Id: 1|| Country name: India Country Id: 2|| Country name: Germany Country Id: 3|| Country name: England Country Id: 4|| Country name: Russia After Sort by name: Country Id: 2|| Country name: England Country Id: 4|| Country name: Germany Country Id: 1|| Country name: India Country Id: 3|| Country name: Russia |
Please go through  top 50 core java interview questions for more interview questions.