aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to implement Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to implement Comparable" Watch "How to implement Comparable" New topic
Author

How to implement Comparable

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
//What changes need to be done if i want to
// implement Comparable.The compiler tells to override "compareTo()"
//method. But what should be its body so that i get the same output
//as obtained by implementing Comparator

import java.util.*;
public class Car implements Comparator {
public int wheels = 0;
public Car(){}
public Car(int wheels) {
this.wheels = wheels;
}
public int compare(Object o1, Object o2) {
Car c1 = (Car)o1;
Car c2 = (Car)o2;
return new Integer(c1.wheels).compareTo(new Integer(c2.wheels));
//if you interchage c1 and c2, you
// will get o/p in descending order
}
public String toString() {
return new Integer(wheels).toString();
}
public static void main(String[] args) {
Car c1 = new Car(1);
Car c2 = new Car(2);
Car c3 = new Car(3);
TreeSet list = new TreeSet(new Car());
list.add(c3);
list.add(c1);
list.add(c2);
Iterator it = list.iterator();
while (it.hasNext())
System.out.print (it.next() + " ");
}
}
/*

Compilation succeeds, the output is 1 2 3
The compilation succeeds and the output will be in ascending order
(e.g.: 1 2 3).
The class Car is a Comparator but at the same time also a
business object that contains the total of wheels for the car.
At line 25 the TreeSet is created, the TreeSet of the constructor
takes in
the Car() Comparator, this constructor creates a new empty set
sorted according to the Car criteria(see Car.txt).
All elements must be mutually comparable by the
car.compare(Object o1, Object o2) method. The Car object used to
create the
TreeSet does not belong to the set.

The cars are sorted by the total amount of wheels.
3 new objects are created and added to a list.
When the TreeSet start sorting it will call
the compare method and it compares the
total of wheels. It uses the Integer compareTo method.
*/


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
With Comparable, instead of comparing 2 objects (as compare in Comparator do), you'll compare "this" object with another one.

So your job for moving from Compartor to Comparable should be easy...


Regards,<br />Seb<br /> <br />SCJP 1.4
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

The Java� Tutorial - Object Ordering
Making Java Objects Comparable
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to implement Comparable