Sunday, May 1, 2016

Comparator and Comparable


The Comparator and Comparable interfaces are two interfaces avaiable in Java for sorting the user defined objects.
For example, if you want to sort the List of Employee Objects based on Employee Id, name, or address, you would need to use either the comparable interface or the comparator interface.

Comparable Vs Comparator

  • Both interfaces are used for comparing two different objects of same class.
  • If the source code of the class(the object about to be sorted) is accessible and modifiable, then we can implement the Comparable interface. Otherwise, if a source code is not available, we can make use of the Comparator interface.
The examples below demostrates how to use the Comparator and Comparable interfaces for sorting an Employee Object by its id.

Using the Comparable Interface 

Overriding the compareTO() method if a source code is available is the proper way to use the comparable interface.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
class Employee implements Comparable {
    int id;
    String name;
     
    public Employee(int id, String name) {
        this.id=id;
        this.name=name;
    }
    @Override
    public int compareTo(Object o) {
        Employee emp = (Employee)o;
        return this.id-emp.id;
    }
}
 
public class EmployeeSorting {
    public static void main(String[] args) {
        List employees = new ArrayList<>();
        Employee emp1 = new Employee(3, "Jerome");
        Employee emp2 = new Employee(1, "Albert");
        Employee emp3 = new Employee(2, "Samiya");
        Employee emp4 = new Employee(5, "Stella");
        Employee emp5 = new Employee(4, "Kent");
        employees.add(emp1);
        employees.add(emp2);
        employees.add(emp3);
        employees.add(emp4);
        employees.add(emp5);
         
        Collections.sort(employees);
         
        for (Employee employee : employees) {
            System.out.println(employee.id + ", " + employee.name);
        }
    }
}
After executing the class in the example above, the output will be as follows:
     1, Albert
     2, Samiya
     3, Jerome
     4, Kent
     5, Stella
The compareTo() method should compare this object to another object and return intValue. Below are the rules for intValue
if the method returns:
  • -ve value, then the object is smaller than another object.
  • 0, then the object value is same as another value.
  • +ve value, then the object is larger than another object.

Using Comparator

When a source code is not available for the object that we want to sort, we can use the comparator interface for sorting.
In this example, we will introduce a new class that implements the comparator interface and override the compare() method and Pass the Comparator object to the sort() method of collections.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
class EmployeeComparator implements Comparator
{
    @Override
    public int compare(Object o1, Object o2) {
        Employee emp1 = (Employee)o1;
        Employee emp2 = (Employee)o2;
        return emp1.id-emp2.id;
    }
}
 
public class EmployeeSorting {
    public static void main(String[] args) {
        List employees = new ArrayList<>();
        Employee emp1 = new Employee(3, "Jerome");
        Employee emp2 = new Employee(1, "Albert");
        Employee emp3 = new Employee(2, "Samiya");
        Employee emp4 = new Employee(5, "Stella");
        Employee emp5 = new Employee(4, "Kent");
        employees.add(emp1);
        employees.add(emp2);
        employees.add(emp3);
        employees.add(emp4);
        employees.add(emp5);
         
        Collections.sort(employees, new EmployeeComparator());
         
        for (Employee employee : employees) {
            System.out.println(employee.id + ", " + employee.name);
        }
    }
}
The compare() method should compare one object with another object and return intValue

No comments:

Post a Comment