1. import java.util.*;
2. public class Car implements Comparator {
3. public int wheels = 0;
4. public Car(){}
5. public Car(int wheels) {
6. this.wheels = wheels;
7. }
8. public int compare(Object o1, Object o2) {
9. Car c1 = (Car)o1;
10. Car c2 = (Car)o2;
11. return new Integer(c1.wheels).compareTo(new Integer(c2.wheels));
12. }
13. public String toString() {
14. return new Integer(wheels).toString();
15. }
16. public static void main(String[] args) {
17. Car c1 = new Car(1);
18. Car c2 = new Car(2);
19. Car c3 = new Car(3);
20. TreeSet list = new TreeSet(new Car());
21. list.add(c3);
22. list.add(c1);
23. list.add(c2);
24. Iterator it = list.iterator();
25. while (it.hasNext())
26. System.out.print (it.next() + " ");
27. }
28. }