Java Comparable Interface Example

In this article, we will discuss how to use the Comparable interface in Java to compare and sort objects. We will cover how to implement the Comparable interface and provide examples to demonstrate its usage.

Table of Contents

  1. Introduction
  2. Implementing Comparable Interface
  3. Sorting a List of Objects Using Comparable
  4. Complete Example
  5. Conclusion

Introduction

The Comparable interface in Java is used to define a natural ordering for the objects of a class. It is part of the java.lang package and provides a way to order the objects of a user-defined class. The interface has one main method:

  • int compareTo(T o): Compares this object with the specified object for order.

Implementing Comparable Interface

To implement the Comparable interface, a class must implement the compareTo method. This method defines the natural ordering of the objects.

Example

Let's create a Student class that implements the Comparable interface and sorts students by their age.

public class Student implements Comparable<Student> {
    String name;
    int age;

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

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

    @Override
    public String toString() {
        return "Student{name='" + name + "', age=" + age + '}';
    }

    public static void main(String[] args) {
        // Example usage in the main method
        List<Student> students = new ArrayList<>();
        students.add(new Student("John", 22));
        students.add(new Student("Alice", 20));
        students.add(new Student("Bob", 21));

        // Sorting the list of students
        Collections.sort(students);
        System.out.println("Sorted students: " + students);
    }
}

Output:

Sorted students: [Student{name='Alice', age=20}, Student{name='Bob', age=21}, Student{name='John', age=22}]

Sorting a List of Objects Using Comparable

Once a class implements the Comparable interface, you can use Collections.sort to sort a list of objects of that class.

Example

Here's an example demonstrating how to sort a list of Student objects using the Comparable interface.

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

public class SortStudentsUsingComparable {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("John", 22));
        students.add(new Student("Alice", 20));
        students.add(new Student("Bob", 21));

        // Sorting the list of students
        Collections.sort(students);
        System.out.println("Sorted students: " + students);
    }
}

class Student implements Comparable<Student> {
    String name;
    int age;

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

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

    @Override
    public String toString() {
        return "Student{name='" + name + "', age=" + age + '}';
    }
}

Output:

Sorted students: [Student{name='Alice', age=20}, Student{name='Bob', age=21}, Student{name='John', age=22}]

Complete Example

Here's a complete example that includes the Student class implementing the Comparable interface and sorting a list of Student objects by age.

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

public class ComparableExample {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("John", 22));
        students.add(new Student("Alice", 20));
        students.add(new Student("Bob", 21));

        // Sorting the list of students
        Collections.sort(students);
        System.out.println("Sorted students: " + students);
    }
}

class Student implements Comparable<Student> {
    String name;
    int age;

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

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

    @Override
    public String toString() {
        return "Student{name='" + name + "', age=" + age + '}';
    }
}

Output:

Sorted students: [Student{name='Alice', age=20}, Student{name='Bob', age=21}, Student{name='John', age=22}]

Conclusion

Using the Comparable interface in Java allows you to define a natural ordering for the objects of a class. By implementing the compareTo method, you can specify the logic for comparing objects. This guide provided examples of how to implement the Comparable interface and sort a list of custom objects. By understanding these concepts, you can efficiently manage and sort collections of custom objects in your applications.

Comments