Saturday, July 14, 2018

Comparable and its example in java

comparable interface and its example

Most of us have heard about comparable and most of you also know what is comparable,
What is a comparable interface?
Comparable interface is used to sort the objects on the basis of any one variable, It is found in java.lang package and contain only one method compareTo(Object o).

In java When we have a scenario where we need to sort the array of objects on the basis of there member variable, or to eligible the objects to compare we use Comparable.

There are some good interview questions are there related to the comparable interface.

In this article, we will see one example of the implementation of the comparable interface and its explanation.

#Objective - We have a list of student and we need to sort the list on the bases of student ages in ascending order.

  • We will implement the Comparable interface in the student class.
  • we will override the compareTo() method and write the logic for that 
  • As compareTo() method return 
    • 1 when own variable is greater than compared object variable.
    • 0 when both are equal
    • -1 when the own variable is less than compared object variable.
Now let see the implementation.
Below is the Student class

package com.programinjava.learning.comparable;

//implementing the comparable interface
public class Student implements Comparable<Student>{
 private int age;
 private String name;

 public int getAge() {
  return age;

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

 public String getName() {
  return name;

 public void setName(String name) { = name;

 public int compareTo(Student o) {
  return this.getAge() - o.getAge();

 public String toString() {
  return "Student [age=" + age + ", name=" + name + "]";

Now we will see the Main Demo class
package com.programinjava.learning.comparable;

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

public class ComparableDemo {
 public static void main(String[] args) {
//  creating list of student with different name and age
  List<Student> list = new ArrayList<>();
  Student s1 = new Student();
  s1.setName("student 1");
//  second
  Student s2 = new Student();
  s2.setName("student 2");
//  third
  Student s3 = new Student();
  s3.setName("student 3");
//  fourth
  Student s4 = new Student();
  s4.setName("student 4");
//  fifth
  Student s5 = new Student();
  s5.setName("student 5");
  System.out.println("Printing before sorting the list");
//  sorting the list 
  System.out.println("Printing after sorting the list");

Result Looks like below
Printing before sorting the list
Student [age=20, name=student 2]
Student [age=10, name=student 1]
Student [age=40, name=student 4]
Student [age=30, name=student 3]
Student [age=50, name=student 5]

Printing after sorting the list
Student [age=10, name=student 1]
Student [age=20, name=student 2]
Student [age=30, name=student 3]
Student [age=40, name=student 4]
Student [age=50, name=student 5]

I hope this will help you in understand how to implement comparable and when to implement it.

There are some other topics if you want to explore

Conditional Logic in java
Loops in java
Arrays in java
Wrapper classes in java
Methods in java
Object Oriented Practices
Inheritance in java

If you have any issue, please leave us a comment, if you like it, please share it with your friends

Thanks for reading 


Post a Comment