- /*
- 验证TreeSet
- */
- import java.util.*;
- class TreeSetDemo
- {
- public static void main(String[] args)
- {
- TreeSet ts= new TreeSet();
- ts.add("adf");
- ts.add("addgdf");
- ts.add("addfd");
- ts.add("ad3e2d");
- ts.add("adf");
- Iterator it = ts.iterator();
- while (it.hasNext())
- {
- System.out.println(it.next());
- }
- }
- }
- class Student
- {
- private String name;
- private int age;
-
- Student(String name,int age){
- this.name=name;
- this.age=age;
- }
- public int compareTo(object obj){
-
- if (!(obj instanceof Student))
- {
- throw new RuntimeException("输入的不是学生");
- }
- Student s= (Student)obj;
- System.out.println(this.name+"....compare...."s.name);
- if (this.age>s.age)
- {
- return 1;
- }
- if (this.age<s.age)
- {
- return -1;
- }
- return this.name.compareTo(s.name);
- }
- }
复制代码 |