package n;
public interface Comparable<Student> {
public int compareTo(Student s);
}
package n;
import java.util.Set;
import java.util.TreeSet;
public class Student implements Comparable<Student> {
int id;
String name;
public Student(int id, String name){
this.id = id;
this.name = name;
}
public int compareTo(Student s){
if(this.id < s.id) return -1;
else if(this.id > s.id) return 1;
else return 0;
}
public String toString(){
return "<" + this.id + "." + this.name + ">";
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
Student[] stud = new Student[]{
new Student(1002, "张三"),
new Student(1003, "李四"),
new Student(1001, "王五")
};
Set<Student> ts = new TreeSet<Student>();
for(int i = 0; i < stud.length; i ++)
ts.add(stud[i]);
System.out.println(ts);
}
}
此用户略帅
相关分类