What is the difference between Comparable and Comparator Interface?


In Java, both the Comparable and Comparator interfaces are used for comparing objects, but they have different ways of achieving this.

Comparable: The Comparable interface is used to define a natural ordering of objects. When a class implements Comparable, it defines a default ordering of objects of that class. The Comparable interface contains a single method, compareTo(), which takes an object of the same type as the implementing class and returns an integer value indicating whether the object is greater than, equal to, or less than the object being compared to. The compareTo() method is used by collections, such as TreeSet and TreeMap, to sort elements.

Comparator: The Comparator interface is used to define a custom ordering of objects that do not implement Comparable or when a different ordering is required. The Comparator interface contains two methods, compare() and equals(). The compare() method takes two objects of the type being compared and returns an integer value indicating whether the first object is greater than, equal to, or less than the second object. The equals() method is used to determine if two comparators are equal.

In summary, the Comparable interface provides a natural ordering of objects, while the Comparator interface provides a custom ordering of objects. The Comparable interface is implemented by the class being compared, while the Comparator interface is implemented by a separate class.

Comparable InterfaceComparator Interface
OrderingNatural orderingCustom ordering
Implemented byThe class being comparedA separate class
MethodcompareTo()compare()
PurposeTo define a default ordering of objectsTo define a custom ordering of objects
UsageUsed by collections like TreeSet and TreeMapUsed to sort objects in collections like ArrayList or arrays
Number of methodsOne - compareTo()Two - compare() and equals()
Return typeintint

Example Program for Comparable

import java.util.*; 
class Person implements Comparable<Person> { 
private String name;
private int age;

public
Person(String name, int age)
{
this.name = name;
this.age = age;
}

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

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

@Override
public int compareTo(Person other) {
return Integer.compare(this.age, other.age);
}
}

public
class ComparableExample {
public static void main(String[] args) {
List<Person> people =
new ArrayList<>();
people.
add(new Person("Alice", 25));
people.
add(new Person("Bob", 30));
people.
add(new Person("Charlie", 20));

Collections.sort(people);

for
(Person person : people) {
System.
out.println(person.getName() + " - " + person.getAge());
}
} }    

Example Program for Comparator

import java.util.*;

class
Person {
private String name;
private int age;

public
Person(String name, int age)
{
this.name = name;
this.age = age;
}

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

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

class
AgeComparator implements Comparator<Person> {

@Override public int compare(Person p1, Person p2) {
return Integer.compare(p1.getAge(), p2.getAge());
}
}

public
class ComparatorExample {

public
static void main(String[] args)
{

List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 30));
people.add(new Person("Charlie", 20));

AgeComparator ageComparator = new AgeComparator();

Collections.sort(people, ageComparator);

for
(Person person : people) {
System.out.println(person.getName() + " - " + person.getAge());
}
} }
The output for both the program is:
Charlie - 20 Alice - 25 Bob - 30

Comments

Popular posts from this blog

Why Pointers are Eliminated in Java?

What is the advancement made in Hashmap in Java 8?

Integer.parseInt(“text”)