Sorting a list using comparator (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.
Comparator interface
The class whose objects to be sorted do not need to implement this interface. Some third class can implement this interface to sort. E.g., xyz class can implement Comparator interface to sort collection of ABC object by id , by name etc
Here is a simple example using comparator interface sorted a simple list with custom objects...
Program :
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionEmployee implements Comparator
 {

    /**
     * @param args
     */
    public int empID;
    public String empName;

    public CollectionEmployee(int eid, String ename) {
        empID = eid;
        empName = ename;
    }

    public int compare(CollectionEmployee object1, CollectionEmployee object2) {
        // TODO Auto-generated method stub
        return ((object1.empID > object2.empID) ? 1 : 0);
    }

    public static void main(String[] args) {
        List
 empList = new ArrayList
();
        empList.add(new CollectionEmployee(100, "saurabh"));
        empList.add(new CollectionEmployee(300, "Anirudh"));
        empList.add(new CollectionEmployee(200, "Babu"));
        System.out.println("----list as it is ------");
        for (CollectionEmployee emp : empList) {
            System.out.println("No: " + emp.empID + " Name: " + emp.empName);
        }
        System.out.println("----Sorted by id------");
        Collections.sort(empList, new CollectionEmployee(200, "hari"));
        for (CollectionEmployee emp : empList) {
            System.out.println("No: " + emp.empID + " Name: " + emp.empName);
        }
        System.out.println("----Sortd by name-----------");
        // Sort by name
        Collections.sort(empList, new CollectionEmployee(200, "hari") {

            @Override
            public int compare(CollectionEmployee o1, CollectionEmployee o2) {
                return o1.empName.compareTo(o2.empName);
            }
        });
        for (CollectionEmployee emp : empList) {
            System.out.println("No: " + emp.empID + " Name: " + emp.empName);
        }
    }

}
Output :
----list as it is ------
No: 100 Name: saurabh
No: 300 Name: Anirudh
No: 200 Name: Babu
----Sorted by id------
No: 100 Name: saurabh
No: 200 Name: Babu
No: 300 Name: Anirudh
----Sortd by name-----------
No: 300 Name: Anirudh
No: 200 Name: Babu
No: 100 Name: saurabh
Reference : [1]


0 comments to "Sorting a list using comparator (Difference between Comparator and Comparable in Java)"

Post a Comment

Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")

Popular Posts

Enter your email address:

Buffs ...

Tags


Powered by WidgetsForFree