import java.util.*;
class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet ts=new TreeSet();
ts.add(new Student("lisi02",22));
//ts.add(new Student("lisi007",20));
//ts.add(new Student("lisi09",19));
//ts.add(new Student("lisi01",40));
Iterator it=ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
/*
for(Iterator it=ts.iterator();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 String getName(){
return name;
}
public int getAge(){
return age;
}
}
这是实现Comparable前的代码。 |