package ming; import java.util.Comparator; import java.util.TreeSet; class M { int age; public M(int age) { this.age = age; } @Override public String toString() { return "M[age:" + this.age + "]"; } } public class TreeSetTest { public static void main(String[] args) { // TODO Auto-generated method stub TreeSet ts = new TreeSet(new Comparator() { //重写compare定制排序方法 public int compare(Object o1, Object o2) { M m1 = (M) o1; M m2 = (M) o2; if (m1.age > m2.age) { return 1; } else if (m1.age == m2.age) { return 0; } else { return -1; } } }); ts.add(new M(5)); ts.add(new M(-3)); ts.add(new M(9)); System.out.println(ts); } }