Java Collection Framework - Comparable

Java Collection Framework - Comparable


Posted in : Core Java Posted on : May 28, 2011 at 4:26 PM Comments : [ 0 ]

Comparable is an interface that is implemented to make an instance of a class comparable. The method compareTo() of this interface compares the two objects of the class.

Java Collection Framework - Comparable

An object is called comparable when the object is able to compare itself with some other object. In Java Comparable is an interface that is implemented to make an instance of a class comparable. The method compareTo() of this interface compares the two objects of the class. As the Object is accepted by the compareTo() method so any type of instance can be passed in it. However, the two Objects can be compared with if they are of same argument type so you will have to make sure that the comparable objects are of same type otherwise, it will throw an exception java.lang.ClassCastException. This method returns the three different Integer values in different conditions if the condition is as follows :

java.lang.Comparable :

    publicÃ? intÃ? compareTo(ObjectÃ? e)Ã? {

� � � � return� 0;

� � }

  • returns + ve if this object is greater than the comparable object.
  • returns zero if this object is equal to the comparable object.
  • returns - ve if this object is less than the comparable object.

 Implementing the Comparable interface Collections.sort or Arrays.sort sorted the elements/objects automatically of a list or arrays. Comparable interface is a part of Java Collection Framework and is available in java.lang package. 

Syntax

public interface Comparable<T>

Methods of Comparable

There is a method of Comparable in java 6 :

  • compareTo() : 

                                    syntax : int compareTo(T  o)

Example

package devmanuals.com;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class ComparableDemo implements Comparable<ComparableDemo>{

  String name;

  ComparableDemo(String n){

    this.name=n;

  }

  public String getName(){

    return name;

  }

  public int compareTo(ComparableDemo e) {

    return (this.name).compareTo(e.name);

  }

  public static void main(String[] args) {

    List<ComparableDemo> l = new ArrayList<ComparableDemo>();

    System.out.println("Elements of list in a sorted order are =");

    l.add(new ComparableDemo("Anger"));

    l.add(new ComparableDemo("Tom"));

    l.add(new ComparableDemo("Titanic"));

    l.add(new ComparableDemo("Dummy"));

    l.add(new ComparableDemo("Bipul"));

    l.add(new ComparableDemo("Vinay"));

    l.add(new ComparableDemo("Rajesh"));

    l.add(new ComparableDemo("Ankit"));

    Collections.sort(l);

  for(ComparableDemo cd : l)

    System.out.println(cd.getName());

  }  
}

Output :

Elements of list in a sorted order are =

Anger

Ankit

Bipul

Dummy

Rajesh

Titanic

Tom

Vinay

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics