Java Comparable Interface

Category: Java   Tags: Java, Java Collection, Java Comparable Interface

A comparable object is capable of comparing itself with another object. A class must implements the java.lang.Comparable interface to compare its instances. Comparable has this declaration:

                                    public interface Comparable<T>
                                

Methods

int compareTo(T o)
It compares this object with the specified object o for ordering.

Returns -ve integer if this object < o
Returns 0 if this object == o
Returns +ve integer if this object > o

Suppose we have an ArrayList whose element type is a custom class Student having fields rollNumber, name, age and we need to sort the array based on age. This is where Comparable comes into the picture. Here is one Java example that shows this:

JavaComparableExample.java

                            package com.tutorial.java.collections;

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

                            class Student implements Comparable<Student> {

                                private int rollNo;
                                private String name;
                                private int age;

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

                                public int getRollNo() {
                                    return rollNo;
                                }

                                public void setRollNo(int rollNo) {
                                    this.rollNo = rollNo;
                                }

                                public String getName() {
                                    return name;
                                }

                                public void setName(String name) {
                                    this.name = name;
                                }

                                public int getAge() {
                                    return age;
                                }

                                public void setAge(int age) {
                                    this.age = age;
                                }

                                @Override
                                public int compareTo(Student o) {
                                    // This will sort in decending order of age
                                    //return o.getAge() - this.age;

                                    // This will sort in ascending order of age
                                    return this.age - o.getAge();
                                }

                            }

                            public class JavaComparableExample {

                                static void details(Student student) {
                                    System.out.println(student.getRollNo() + " - " + student.getName() + " - " + student.getAge());
                                }

                                public static void main(String[] args) {
                                    ArrayList<Student> list = new ArrayList<Student>();
                                    list.add(new Student(2, "B", 16));
                                    list.add(new Student(1, "A", 18));
                                    list.add(new Student(4, "D", 17));
                                    list.add(new Student(3, "C", 15));

                                    System.out.println("Original ArrayList: ");
                                    for (int i = 0; i < list.size(); i++) {
                                        details(list.get(i));
                                    }

                                    Collections.sort(list);

                                    System.out.println("Sorted by age");
                                    for (int i = 0; i < list.size(); i++) {
                                        details(list.get(i));
                                    }
                                }

                            }
                        

Output:

                            Original ArrayList:
                            2 - B - 16
                            1 - A - 18
                            4 - D - 17
                            3 - C - 15
                            Sorted by age
                            3 - C - 15
                            2 - B - 16
                            4 - D - 17
                            1 - A - 18