Thursday 19 February 2015

Difference between Java Comparator and Comparable interfaces

This post explains the difference between most important java interfaces used for sorting namely Comparator and Comparable.
http://www.javacodegeeks.com/2013/03/difference-between-comparator-and-comparable-in-java.html
http://www.programcreek.com/2011/12/examples-to-demonstrate-comparable-vs-comparator-in-java/
ComparableComparator
Class whose objects which we need to sort must implement java.lang.Comparable interface compareTo(Object o1) method
E.g.
If we want to sort Employee, our employee class needs to implement above method.
Class whose objects which we need to sort do not need to implement any interface method.
E.g. 
If we want to sort Employee objects using employee name, We write a class EmpSortByName which implements java.lang.Comparable interfacecompare(Object o1,Object o2) method
Sorting logic is in the 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 name, last name, age etc.
We use Collections.sort(List) method to sorting.
E.g
Collections.sort(list);
We must use the Collections.sort(List, Comparator) for sorting.
E.g.
Collections.sort(list, new EmpSortByName());
java.lang.Comparable: int compareTo(Object o1)
This method compares this object with o1 object. Returned int value has the following meanings.
1. positive – this object is greater than o1
2. zero – this object equals to o1
3. negative – this object is less than o1
java.lang.Comparator: int compare(Object o1, Objecto2)
This method compares o1 and o2 objects. Returned int value has the following meanings.
1. positive – o1 is greater than o2
2. zero – o1 equals to o2
3. negative – o1 is less than o1
-----------------------------------------------------------------------------------------------------------------------\
EX:-
public class ComparatorDemo {

    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
                new Person("Joe", 24),
                new Person("Pete", 18),
                new Person("Chris", 21)
        );
        Collections.sort(people, new LexicographicComparator());
        System.out.println(people);
        Collections.sort(people, new AgeComparator());
        System.out.println(people);
    }
}

class LexicographicComparator implements Comparator<Person> {
    @Override
    public int compare(Person a, Person b) {
        return a.name.compareToIgnoreCase(b.name);
    }
}

class AgeComparator implements Comparator<Person> {
    @Override
    public int compare(Person a, Person b) {
        return a.age < b.age ? -1 : a.age == b.age ? 0 : 1;
    }
}

class Person {

    String name;
    int age;

    Person(String n, int a) {
        name = n;
        age = a;
    }

    @Override
    public String toString() {
        return String.format("{name=%s, age=%d}", name, age);
    }
}

No comments:

Post a Comment