Thursday, July 14, 2016

Difference between comparable and comparator

java.util.Comparator 

java.lang.Comparable

Some time you write code to sort object of a class for which you are not the original author, or you don't have access to code. In these cases you can not implement Comparable and Comparator is only way to sort those objects.



1) Comparable provides single sorting sequence. In other words, we can sort the collection on the basis of single element such as id or name or price etc.       
Comparator provides multiple sorting sequence. In other words, we can sort the collection on the basis of multiple elements such as id, name and price etc.

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.

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;
    }
    
}

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 ;
    }
    
}



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

No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...