For more information please go through http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

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

Prerequisites

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) {
		this.name = name;
		this.age = age;
		this.email = email;
	}

	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;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public int compareTo(final Person o) {
		return age - o.age;
	}

	@Override
	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, "p1@email.com");
		Person p2 = new Person("P2", 28, "p2@email.com");
		Person p3 = new Person("P3", 24, "p3@email.com");
		Person p4 = new Person("P4", 32, "p4@email.com");

		List<Person> persons = new ArrayList<Person>();
		persons.add(p1);
		persons.add(p2);
		persons.add(p3);
		persons.add(p4);

		Collections.sort(persons);

		for (Person person : persons) {
			System.out.println(person);
		}
	}

}

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

Name : P1, Age : 20, Email : p1@email.com
Name : P3, Age : 24, Email : p3@email.com
Name : P2, Age : 28, Email : p2@email.com
Name : P4, Age : 32, Email : p4@email.com

That’s all. Thank you for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

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