别写排序了,用TreeSet


从一本书看到,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
 
 

转载于:https://www.cnblogs.com/zzqiltw/p/3355142.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值