Monday, May 04, 2015

What is the use of comparable and comparator interfaces in java.When should developer use the same.



What is the use of comparable and comparator interfaces in java.When should developer use the same.

Sorting is one of the main event which any developer need to face in his carrier. That sorting might be on int value or string value, in array, or Collections etc.

Java API gives us beautiful two interface comparable and comparator which assist doing the same.

Comparable interface contains method compareTo(Object  o) which is used to compare the same class Object.

Comparator interface contain method compare(Object  o1, Object  o2) which is used to compare two different object either of same class or of different class.

Lets apply force and try to understand use of this interface using simple java programe.

Lets say we had class Student having id and name as a field.

We got a requirement in which we need to sort this Student class object filled in collections  in both way by id and by name.

Lets  go first with sorting by default using id

public class Student implements Comparable {
    private int id = 0;
    private String firstName = null;
    private String lastName = null;


    public Student(int id, String fName, String lName) {
        this.id = id;
        this.firstName = fName;
        this.lastName = lName;
        
    }

    @Override
    public int compareTo(Student o) {
        return this.id - o.id;
    }

    @Override
    public String toString() {
        return "Student : " + id + " - " + firstName + " - " + lastName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestSorting {
    public static void main(String[] args) {
    Student e1 = new Student(1, "afirstname", "aLastName");
    Student e2 = new Student(2, "cfirstname", "cLastName");
        Student e3 = new Student(3, "bfirstname", "bLastName");
         
        List Students = new ArrayList();
        Students.add(e2);
        Students.add(e3);
        Students.add(e1);
         
        // UnSorted List
        System.out.println(Students);

        Collections.sort(Students);
        // Default Sorting by Student id
        System.out.println(Students);
    }
}


Out Put:-

[Student : 2 - cfirstname - cLastName, Student : 3 - bfirstname - bLastName, Student : 1 - afirstname - aLastName]
[Student : 1 - afirstname - aLastName, Student : 2 - cfirstname - cLastName, Student : 3 - bfirstname - bLastName]


Now lets check the condition when we need to sort the same student class by name i.e. firstName and lastName.

For this we need to use Comparator interface having compareTo(Object o1, Object o2).


Lets us create two class

import java.util.Comparator;

public class FirstNameSorter implements Comparator{

@Override
public int compare(Student o1, Student o2) {
return o1.getFirstName().compareTo(o2.getFirstName());
}
}


import java.util.Comparator;

public class LastNameSorter implements Comparator {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getLastName().compareTo(o2.getLastName());
    }

}

public class Student implements Comparable {
    private int id = 0;
    private String firstName = null;
    public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

private String lastName = null;
    

    public Student(int id, String fName, String lName) {
        this.id = id;
        this.firstName = fName;
        this.lastName = lName;
        
    }

    @Override
    public int compareTo(Student o) {
        return this.id - o.id;
    }

    @Override
    public String toString() {
        return "Student : " + id + " - " + firstName + " - " + lastName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    
}

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestSorting {
public static void main(String[] args) {
    Student e1 = new Student(1, "afirstname", "aLastName");
    Student e2 = new Student(2, "cfirstname", "cLastName");
        Student e3 = new Student(3, "bfirstname", "bLastName");
         
        List Students = new ArrayList();
        Students.add(e2);
        Students.add(e3);
        Students.add(e1);

        // UnSorted List
        System.out.println(Students);

        Collections.sort(Students);
        // Default Sorting by Student id
        System.out.println(Students);

        Collections.sort(Students, new FirstNameSorter());
        // Sorted by firstName
        System.out.println(Students);

        Collections.sort(Students, new LastNameSorter());
        // Sorted by lastName
        System.out.println(Students);
         
    }
}




OUT PUT:- 
[Student : 2 - cfirstname - cLastName, Student : 3 - bfirstname - bLastName, Student : 1 - afirstname - aLastName]
[Student : 1 - afirstname - aLastName, Student : 2 - cfirstname - cLastName, Student : 3 - bfirstname - bLastName]
[Student : 1 - afirstname - aLastName, Student : 3 - bfirstname - bLastName, Student : 2 - cfirstname - cLastName]
[Student : 1 - afirstname - aLastName, Student : 3 - bfirstname - bLastName, Student : 2 - cfirstname - cLastName]

No comments: