Introduction
The Comparable interface in Java is used to define the natural ordering of objects. It allows objects to be compared and sorted using the compareTo() method.
Table of Contents
- What is
Comparable? - Implementing
Comparable - Different Use Cases
- Conclusion
1. What is Comparable?
Comparable is an interface that allows objects to be compared to each other, defining a natural order. It contains the compareTo(T o) method, which returns a negative integer, zero, or a positive integer depending on whether the object is less than, equal to, or greater than the specified object.
2. Implementing Comparable
To use Comparable, a class must:
- Implement the
Comparableinterface. - Override the
compareTo(T o)method to provide comparison logic.
3. Different Use Cases
Use Case 1: Sorting a List of Strings
This example demonstrates how Comparable is used to sort a list of strings in natural order.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StringComparableExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Anil");
names.add("Raj");
names.add("Kumar");
Collections.sort(names);
System.out.println("Sorted names: " + names);
}
}
Output:
Sorted names: [Anil, Kumar, Raj]
Use Case 2: Custom Object Sorting by Single Field
Here, we implement Comparable in a custom class to sort by a single field, such as age.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Person implements Comparable<Person> {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person other) {
return Integer.compare(this.age, other.age);
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
public class PersonComparableExample {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Anil", 30));
people.add(new Person("Raj", 25));
people.add(new Person("Kumar", 35));
Collections.sort(people);
System.out.println("Sorted by age: " + people);
}
}
Output:
Sorted by age: [Raj (25), Anil (30), Kumar (35)]
Use Case 3: Sorting by Multiple Fields
This example shows sorting by multiple fields, such as age and then name.
class Employee implements Comparable<Employee> {
String name;
int age;
Employee(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Employee other) {
int ageComparison = Integer.compare(this.age, other.age);
if (ageComparison != 0) {
return ageComparison;
}
return this.name.compareTo(other.name);
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
public class EmployeeComparableExample {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("Anil", 30));
employees.add(new Employee("Raj", 25));
employees.add(new Employee("Kumar", 30));
Collections.sort(employees);
System.out.println("Sorted by age and name: " + employees);
}
}
Output:
Compilation failed.
4. Conclusion
The Comparable interface in Java provides a way to define the natural ordering of objects, facilitating sorting and comparison. By implementing Comparable and overriding the compareTo() method, you can enable sorting by various criteria, enhancing the functionality of your Java applications.