在使用set集合时,set中存储的数据为对象,如果对象为自定义对象,则使用TreeSet()类时,系统执行排序时,将会不知道按照你的那个字段进行排序 所以需要重写compareTo()函数。
class student implements Comparable<Object>{
public String name
;
public int age
;
public int compareTo(Object o
){
student stu
=(student
)o
;
int result
= age
>stu
.age
?1:(age
==stu
.age
?0:-1);
return result
;
}
}
继承的接口,并重写方法,需要在自己定义的类体中写。