collec

collec

actually both comparator and comparable interfaces are using for sorting, when exactly we go for comparator and when exactly we go for comparable?

View Answers

March 15, 2012 at 12:39 PM

A comparable object is capable of comparing itself with another object. The Comparable class itself implements the java.lang.Comparable interface in order to compare its instances. A class implementing Comparable interface need to override compareTo(Object obj) method and put the logic for sorting.

A comparator object is capable of comparing two different objects. TheComparator class does not compare its instances, but some other class instance. Comparator interface is used when an extra logic is required to sort the objects. One need to override compare(Object obj1, Object obj2) method.For example you want the list of Person object to be sorted on the basis of complete name i.e "name lastName" but also on the other hand doesnt want to change the Person class default sorting implementation or Person class is a jar so so no code modification in it can be done.


March 15, 2012 at 12:53 PM

Comparable Example

import java.util.*;

class Person implements Comparable{
    int age;

    public void setAge(int age){    
        this.age=age;
    }
    public int getAge(){    
        return this.age;    
    }

    public int compareTo(Object ob){
        if(!(ob instanceof Person)){
            throw new ClassCastException("Invalid object");
        }

        int age = ((Person) ob).getAge();

        if(this.getAge() > age)    
            return 1;
        else if ( this.getAge() < age )
            return -1;
        else
            return 0;

    }
 }
 public class ComparableExample{

    public static void main(String args[]){

        Person one = new Person();        
        one.setAge(35);

        Person two = new Person();        
        one.setAge(30);

        if(one.compareTo(two) > 0) {        
            System.out.println("Person one is elder than Person two!");

        } else if(one.compareTo(two) < 0) {        
            System.out.println("Person one is younger than Person two!");        

        } else if(one.compareTo(two) == 0) {        
            System.out.println("Both Persons are same!");        
        }
     }
 }

Comparator Example

import java.util.*;

class Person{
    int age;    
    String name;

    public void setAge(int age){
        this.age=age;    
    }
    public int getAge(){
        return this.age;    
    }
    public void setName(String name){
        this.name=name;    
    }
    public String getName(){    
        return this.name;    
    }
}
 class AgeComparator implements Comparator{

    public int compare(Object ob1, Object ob2){
        int ob1Age = ((Person)ob1).getAge();        
        int ob2Age = ((Person)ob2).getAge();

        if(ob1Age > ob2Age)
            return 1;
        else if(ob1Age < ob2Age)
            return -1;
        else
            return 0;    
    }
 }
 public class ComparatorExample{
       public static void main(String args[]){
        Person person[] = new Person[3];

        person[0] = new Person();
        person[0].setAge(35);
        person[0].setName("A");

        person[1] = new Person();
        person[1].setAge(30);
        person[1].setName("B");

        person[2] = new Person();
        person[2].setAge(32);
        person[2].setName("C");

        System.out.println("Order of person before sorting is");
        for(int i=0; i < person.length; i++){
            System.out.println( person[i].getName() + "\t" + person[i].getAge());
        }

        Arrays.sort(person, new AgeComparator());
        System.out.println("\n\nOrder of person after sorting by person age is");

        for(int i=0; i < person.length; i++){
            System.out.println( person[i].getName() + "\t" + person[i].getAge());
        }
       }
    }









Related Tutorials/Questions & Answers:
collec

Ads