从一本书看到,TreeSet是一个可以自动排序的集合.将数据或者对象添加进去后,会自动给我们排好序。虽然有类似的sort函数,但是用这个集合存储数据和整理数据都比较方便。

1.自带的默认排序算法排序,例如:

 
import java.util.TreeSet;
 
import org.junit.Test;
 
 
public class TestTreeSet {
    @Test
    public void testTreeSet() {
        TreeSet<Integer> ts = new TreeSet<Integer>();
    
        ts.add(10);
        ts.add(6);
        ts.add(8);
        ts.add(7);
        
        for (int i : ts) {
            System.out.println(i);
        }
    } 
}

 

 
 
JUnit测试结果正常,输出结果:
 
6
7
8
10
 
 
 

简直是那些说是懒得写快排其实是写不出快排的人比如说我的必备啊!

2.人工地自定义排序,需要实现Comparator接口。
举个最简单的例子,比如说写一个按照学生的学分绩给学生排序的程序。

先定义一个学生类: 

 
package domain;
 
public class Student {
 
    private String sname;            //student name
    private int score;                    //student score
 
    private String sno;                    //student number
    
    public Student(String sname, int score, String sno) {
        this.sname = sname;
        this.score = score;
        this.sno = sno;
    }
    
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
 
    public String toString() {
        return this.sname + " " + this.sno +" "+ this.score;            //重写toString方法输出方便
    }
}

 

 
 
再实现Comparator接口:
 
package domain;
 
public class Student {
 
    private String sname;            //student name
    private int score;                    //student score
 
    private String sno;                    //student number
    
    public Student(String sname, int score, String sno) {
        this.sname = sname;
        this.score = score;
        this.sno = sno;
    }
    
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
 
    public String toString() {
        return this.sname + " " + this.sno +" "+ this.score;            //重写toString方法输出方便
    }
}

 

 然后就可以加入集合,轻松愉快地跳过各种排序,直接输出就可以了:
 
 
package domain;
 
public class Student {
 
    private String sname;            //student name
    private int score;                    //student score
 
    private String sno;                    //student number
    
    public Student(String sname, int score, String sno) {
        this.sname = sname;
        this.score = score;
        this.sno = sno;
    }
    
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
 
    public String toString() {
        return this.sname + " " + this.sno +" "+ this.score;            //重写toString方法输出方便
    }
}

 

 
 

输出结果:

 
1110330130 bbb 95
1110330160 eee 97
1110330140 ccc 98
1110330150 ddd 98
1110330116 aaa 100
 
 
posted on 2013-10-07 11:18  zzqiltw  阅读(197)  评论(0编辑  收藏  举报