import java.util.*;
class TreeSetDemo {
public static void main(String[] args) {
TreeSet ts =
new TreeSet(
new Mycompare());
ts.add(
new People(
"a02",
3));
ts.add(
new People(
"a01",
5));
ts.add(
new People(
"a39",
9));
ts.add(
new People(
"a40",
7));
System.out.println(ts);
Iterator i = ts.iterator();
while(i.hasNext()) {
People p = (People) i.next();
System.out.println(p.getName() +
"....." + p.getAge());
}
}
}
class People implements Comparable {
private String name;
private int age;
People (String name,
int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge(){
return this.age;
}
public int compareTo (Object obj) {
People p = (People) obj;
if (
this.age > p.age) {
return 1;
}
else if (
this.age == p.age){
return this.name.compareTo(p.name);
}
return -
1;
}
}
class Mycompare implements Comparator {
public int compare(Object o1, Object o2) {
People p1 = (People) o1;
People p2 = (People) o2;
int num = p1.getName().compareTo(p2.getName());
if (num ==
0) {
return p1.getAge() - p2.getAge();
}
return num;
}
}