• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparable interface

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any body explain me Comparable interface with an example or can give a link for the example ...

thanks a lot .
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am attaching an example, which shows how to use Comparable interface. In this example, I am comparing Rabbits based on their ear length. Let me know if you have any questions over this example:

/**
* @author Srinivas Velamuri
*
*/
public class ComparableExample {


public static void main(String[] args) {
Rabbit myRabbit = new Rabbit(7); // my Rabbit with 7 inches ears
Rabbit[] rabbits = new Rabbit[10];
for (int i=0; i < rabbits.length; i++) {
rabbits[i] = new Rabbit(i+2);
if ( myRabbit.compareTo(rabbits[i]) > 0) {
System.out.println ("myRabbit is bigger than the rabbit at position " + i + " in the Rabbit collection");
} else if ( myRabbit.compareTo(rabbits[i]) < 0) {
System.out.println ("myRabbit is smaller than the rabbit at position " + i + " in the Rabbit collection");
} else {
System.out.println ("myRabbit is same than the rabbit at position " + i + " in the Rabbit collection");
}

}

}

}

class Rabbit implements Comparable {
private int earLength; // in inches

Rabbit(int earLength){ // constructor
this.earLength = earLength;
}

public int compareTo(Object anotherRabbit) {
if (this.earLength > ((Rabbit)anotherRabbit).getEarLength()) {
return 1;
} else if (this.earLength < ((Rabbit)anotherRabbit).getEarLength()) {
return -1;
} else {
return 0;
}
}

public int getEarLength() {
return earLength;
}

}
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you know Java sorts objects on their natural order when inserting them into a sorted collection.
When no natural order is readily apparent (which means in just about every case except for built-in things like String or primitive wrappers) you can implement Comparable to provide the VM with instructions on what that natural order is.

For this purpose Comparable defines the int compareTo(Object o) method.
This method is to be defined to return a positive integer value in case the object being compared TO is smaller than the object being compared on, 0 in case they are identical for sorting order, and negative if the compared on object is the smaller one.

After that everything is pretty much automatic.

What I sometimes do is implement compareTo and then use the result of that in equals, returning false whenever the result of compareTo is not 0 (and of course returning false in case comparison isn't possible, which in case of compareTo should throw a ClassCastException).
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Srinivas & Jeroen ,

As per your explanation I also made a simple example :


thanks once again .
 
reply
    Bookmark Topic Watch Topic
  • New Topic