Powered By Blogger

Thursday, November 22, 2012

Comparable and Comparator example

Comparable

/* Source Code- Example */

/* ------------ Sorting thru Comparable Interface (Natural Sorting) ---------
 * Implements mandatory Comparable interface
 */
class Person implements Comparable<Object>{
   
    String firstname;
    String lastname;
    public Person(String firstname, String lastname) {
        super();
        this.firstname = firstname;
        this.lastname = lastname;
    }
   
    public String getFirstname() {
        return firstname;
    }
    public String getLastname() {
        return lastname;
    }
    /*
     * return -1 : If this object is lesser than the passed object       
        return  0 : If this object is same the passed object
       return  1 : If this object is greater than the person */

    public int compareTo(Object obj){
        Person person=(Person) obj;
        return this.firstname.compareTo(person.getFirstname());
    }
}

public class PersonComparable {
   
    public static void main(String[] args) {
       
        List<Person> person=new ArrayList<Person>();
       
        person.add(new Person("Rajiv","Srivastava"));
        person.add(new Person("Akshay","Kumar"));
        person.add(new Person("Prashant","Gupta"));
       
        /* Sorting- sort method will use compareTo(Object obj) override implementation for natural sorting
         */

        Collections.sort(person);
       
        for (Person p:person){
            System.out.println(p.getFirstname()+" "+p.getLastname());
        }
    }
}

/* Result:
Akshay Kumar
Prashant Gupta
Rajiv Srivastava
*/
--------------------------------------------------------------------------------------------
/* ------------ Sorting thru Comparator Interface (Custom Sorting)
 * Implements mandatory Comparator interface
 */

Comparator:

public class MyCustomComparator implements Comparator<Object> {

    public int compare(Object obj1, Object obj2){
        Empl p1=(Empl) obj1;
        Empl p2=(Empl) obj2;
       
        String p1name=p1.getFirstname()+" "+p1.getLastname();
        String p2name=p2.getFirstname()+" "+p2.getLastname();
       
        return p1name.compareTo(p2name);
    }

    public static void main(String[] args) {
         List <Empl> plist= new ArrayList<Empl>();
       
         plist.add(new Empl("Arvind","Upadhyay"));
         plist.add(new Empl("Arvind","Tendulkar"));
         plist.add(new Empl("Arvind","Kejriwal"));
       
         Collections.sort(plist, new MyCustomComparator());
       
         for(Empl p:plist){
             System.out.println(p.firstname+ " "+p.getLastname());
         }
    }
   
}

class Empl{
   
    String firstname;
    String lastname;
    public Empl(String firstname, String lastname) {
        super();
        this.firstname = firstname;
        this.lastname = lastname;
    }
   
    public String getFirstname() {
        return firstname;
    }
    public String getLastname() {
        return lastname;
    }
}

/* Output:
 * Arvind Kejriwal
    Arvind Tendulkar
    Arvind Tripathi */