package study;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet set=new TreeSet(new MyComparatorp());
person p1=new person(10);
person p2=new person(20);
person p3=new person(40);
person p4=new person(30);
set.add(p1);
set.add(p2);
set.add(p3);
set.add(p4);
System.out.println(set);
}
}
class person{
int score;
public person(int score){
this.score=score;
}
@Override
public String toString() {
return "person [score=" + score + "]";
}
}
class MyComparatorp implements Comparator{
@Override
public int compare(Object o1, Object o2) {
person p1=(person)o1;
person p2=(person)o2;
return p1.score-p2.score;
}
}