Comparable interface in Java

For more information please go through

This interface imposes a natural ordering on objects of each class that implements it and the class’s compareTo method is referred to as its natural comparison method.

Lists or arrays of objects that implement this interface can be sorted automatically by Collections.sort or Arrays.sort respectively. Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set.

The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.

It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave “strangely” when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.

The class which implements Comparable interface must override the below method
int compareTo(T o)
o – the object to be compared.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

We will now see how we can use Comparable interface in the following example


The following things are required in order to run the application

Eclipse Helios
JDK 1.6 or higher

Step 1. Create the below POJO class which implements Comparable interface

package com.roytuts.comparable;
public class Person implements Comparable<Person> {
	private String name;
	private int age;
	private String email;
	public Person(String name, int age, String email) { = name;
		this.age = age; = email;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public int getAge() {
		return age;
	public void setAge(int age) {
		this.age = age;
	public String getEmail() {
		return email;
	public void setEmail(String email) { = email;
	public int compareTo(final Person o) {
		return age - o.age;
	public String toString() {
		return "Name : " + name + ", Age : " + age + ", Email : " + email;

Step 2. Create a test class with below source code

package com.roytuts.comparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ComparableTest {
	 * @param args
	public static void main(String[] args) {
		Person p1 = new Person("P1", 20, "");
		Person p2 = new Person("P2", 28, "");
		Person p3 = new Person("P3", 24, "");
		Person p4 = new Person("P4", 32, "");
		List<Person> persons = new ArrayList<Person>();
		for (Person person : persons) {

Step 3. Run above class and you will get the below output

Name : P1, Age : 20, Email :
Name : P3, Age : 24, Email :
Name : P2, Age : 28, Email :
Name : P4, Age : 32, Email :

That’s all. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *