Skip to main content

Comparable Vs Comparator

Comparable


It Compare an object with other objects of the same type. It contains only one method, i.e., compareTo().

Comparable is not capable of sorting the objects on its own, but the interface defines a method int compareTo() which is responsible for sorting.

compareTo() method is used to compare the given object with the current object and it returns an int value. The value can be either positive, negative, or zero.

public class Student implements Comparable {
private String name;
private int age;

public Student(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
@Override
public String toString() {
return "";
}
@Override
public int compareTo(Student per) {
if(this.age == per.age) return 0;
else return this.age > per.age ? 1 : -1;
}

public static void main(String[] args) {
Person e1 = new Person("Adam", 45);
Person e2 = new Person("Steve", 60);
int retval = e1.compareTo(e2);
System.out.println(retval);
}

Comparator


A Comparator interface is used to order the objects of a specific class. It contains two methods.

1. compare(Object obj1,Object obj2)

2. equals(Object element).

The first method, compare(Object obj1,Object obj2) compares its two input arguments and showcase the output. It returns a negative integer, zero, or a positive integer to state whether the first argument is less than, equal to, or greater than the second.

The second method, equals(Object element), requires an Object as a parameter and shows if the input object is equal to the comparator. The method will return true, only if the mentioned object is also a Comparator. The order remains the same as that of the Comparator.

import java.util.Comparator;

public class School {

private int num_of_students;
private String name;

public School(String name, int num_of_students) {
this.name = name;
this.num_of_students = num_of_students;
}

public int getNumOfStudents() {
return this.num_of_students;
}
public String getName() {
return this.name;
}
}

public class SortSchools implements Comparator {

@Override
public int compare(School sch1, School sch2) {
if(sch1.getNumOfStudents()== sch2.getNumOfStudents()) return 0;
else return sch1.getNumOfStudents() > sch2.getNumOfStudents() ? 1 : -1;
}

public static void main(String[] args) {
School sch1 = new School("sch1", 20);
School sch2 = new School("sch2", 15);
SortSchools sortSch = new SortSchools();
int retval = sortSch.compare(sch1, sch2);
System.out.println(retval);
}
}

Differences

info

Differences: Comparable Vs Comparator

Comparable is present in java.lang package. ======= A Comparator is present in the java.util package.

Comparable affects the original class, i.e., the actual class is modified. ====== Comparator doesn’t affect the original class

Comparable interface is used to sort the objects with natural ordering.===== Comparator in Java is used to sort attributes of different objects.

Comparable provides compareTo() method to sort elements. ===== Comparator provides compare() method, equals() method to sort elements.