package com.fuxi.test.collection;
/**
* 用树集TreeSet可以自动按照某个字段从低到高存放对象类型数据
*/
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<Student2> tree = new TreeSet<Student2>();
Student2 st1,st2,st3,st4;
st1 = new Student2(90,"zhanyi");
st2 = new Student2(100,"zhaner");
st3 = new Student2(53,"zhansan");
st4 = new Student2(67,"zhansi");
tree.add(st1);
tree.add(st2);
tree.add(st3);
tree.add(st4);
Iterator<Student2> it = tree.iterator();
while(it.hasNext()){
Student2 stu = it.next();
System.out.println(""+stu.name+","+stu.english);
}
}
}
class Student2 implements Comparable{
int english = 0;
String name;
public Student2(int english,String name){
this.english = english;
this.name = name;
}
public int compareTo(Object b) {
Student2 student = (Student2) b;
return (this.english-student.english);
}
/*public int compareTo(Student2 st) {
//Student2 st = (Student2) b;
return (this.english-st.english);
}
*/
}