- public class StudentDemo implements Comparable<StudentDemo>
- {
- private String name;
- private int age;
- private int score;
- StudentDemo(String name,int age,int score)
- {
- this.name = name;
- this.age = age;
- this.score = score;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- public void setScore()
- {
- this.score = score;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- public int getScore()
- {
- return score;
- }
- public int compareTo(StudentDemo s)
- {
- int num = new Integer(this.score).compareTo(new Integer(s.score));
-
- if(num == 0)
- {
- return this.name.compareTo(s.name);
- }
- return num;
- }
-
-
-
-
- }
- import java.util.*;
- public class TreeSetSort
- {
-
- public static void main(String[] args)
- {
- TreeSet<StudentDemo> ts = new TreeSet<StudentDemo>();
- ts.add(new StudentDemo("zhangsan2",22,80));
- ts.add(new StudentDemo("zhangsan2",22,80));
- //ts.add(new StudentDemo("zhangsan1",20,90));
- //ts.add(new StudentDemo("zhangsan3",24,70));
- //ts.add(new StudentDemo("zhangsan4",19,95));
- //ts.add(new StudentDemo("zhangsan5",28,63));
-
- Iterator it = ts.iterator();
- while(it.hasNext())
- {
- StudentDemo stu = (StudentDemo)it.next();
- System.out.println(stu.getName()+":"+stu.getAge()+"..."+stu.getScore());
- }
- }
-
- }
复制代码 我添加了2个元素,都是一样的,打印只出现一个。是哪一部分代码的功能是屏蔽相同的? |