equals()在对象中可用于对象唯一性的判断,而compareTo()和compare()方法则是用于比较对象的具体属性; 
compareTo()是一个对象实现Comparable接口而复写的方法, 
而compare()则是一个自定义的比较器实现Comparator接口要实现的方法; 
上代码: 
 
- package jichuceshiheji;
 
 - import java.util.*;
 
 - /*
 
 -  * 16、 声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)。
 
 -  */
 
 - public class Ceshi16 {
 
 -         public static void main(String[] args) 
 
 -         {
 
 -                 //此处默认调用Student1的compareTo方法
 
 -                 //TreeSet<Student1> ts=new TreeSet<Student1>();
 
 -                 //若传入scoreComparator比较器,则会按照scoreComparator的compare方法排序
 
 -                 TreeSet<Student1> ts=new TreeSet<Student1>(new scoreComparator());
 
 -                 
 
 -                 //把学生对象添加到集合
 
 -                 ts.add(new Student1("zhangsan1",20,88));
 
 -                 ts.add(new Student1("zhangsan2",21,90));
 
 -                 ts.add(new Student1("zhangsan3",22,89));
 
 -                 ts.add(new Student1("zhangsan4",19,88));
 
 -                 ts.add(new Student1("zhangsan5",20,85));
 
 -                 
 
 -                 System.out.println("名字\t\t年龄\t分数");
 
 -                 //按照成绩排序输出结果,如果分数相同,则再比较名字
 
 -                 for(Student1 s:ts)
 
 -                         System.out.println(s);
 
 -         }
 
 - }
 
  
- class scoreComparator implements Comparator<Student1>
 
 - {
 
 -         //复写比较器的方法
 
 -         public int compare(Student1 o1, Student1 o2) 
 
 -         {
 
 -                 if(o1.getScore()>o2.getScore())
 
 -                         return 1;
 
 -                 else if(o1.getScore()<o2.getScore())
 
 -                         return -1;
 
 -                 else
 
 -                         return o1.getName().compareTo(o2.getName());//如果分数相同,则再比较名字
 
 -         }
 
 - }
 
 - class Student1 implements Comparable
 
 - {
 
 -         private String name;
 
 -         private int age;
 
 -         private int score;
 
 -         Student1(String name,int age,int score)
 
 -         {
 
 -                 this.name=name;
 
 -                 this.age=age;
 
 -                 this.score=score;
 
 -         }
 
 -         public String getName()
 
 -         {
 
 -                 return name;
 
 -         }
 
 -         public int getAge()
 
 -         {
 
 -                 return age;
 
 -         }        
 
 -         public int getScore()
 
 -         {
 
 -                 return score;
 
 -         }
 
 -         //确保元素唯一
 
 -         public boolean equals(Object obj)
 
 -         {
 
 -                 if(!(obj instanceof Student1))
 
 -                         throw new ClassCastException("类型非法");
 
 -                 Student1 s=(Student1)obj;//把obj强转成Student1类型
 
 -                 //若姓名,年龄,成绩均相同,则视为同一人
 
 -                 return this.name.equals(s.name) && this.age==s.age && this.score==s.score;
 
 -         }
 
 -         //先按年龄排序,若相同,则再按姓名排。
 
 -         public int compareTo(Object obj)
 
 -         {
 
 -                 if(!(obj instanceof Student1))
 
 -                         throw new ClassCastException("类型非法");
 
 -                 Student1 s=(Student1)obj;//把obj强转成Student1类型
 
 -                 
 
 -                 if(this.age>s.age)
 
 -                         return 1;
 
 -                 else if(this.age<s.age)
 
 -                         return -1;
 
 -                 else
 
 -                         return this.name.compareTo(s.name);
 
 -         }
 
 -         public String toString()
 
 -         {
 
 -                 return name+"\t"+age+"\t"+score;
 
 -         }
 
 - }
 
  复制代码 
希望对新人有所帮助。。。 
不足之处,欢迎指正。。。 
 |   
        
 
    
    
    
     
 
 |