Comparator与Comparable,自定义排序和类比较器,TreeSet对象排序

复制代码
/**
 * 学生类
 * @author Administrator
 *
 */
public class Student {

    private String sno ;
    private String sname ;
    private Integer score ;
    
    public Student(String sno, String sname, Integer score) {
        super();
        this.sno = sno;
        this.sname = sname;
        this.score = score;
    }
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public Integer getScore() {
        return score;
    }
    public void setScore(Integer score) {
        this.score = score;
    }
    
import java.util.Comparator;
复制代码
复制代码
/**
 * 比较算法的类,比较器
 * @author Administrator
 *
 */
public class MyCmp implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        // TODO Auto-generated method stub
        if (o1.getScore() > o2.getScore()) {
            return 1;
        } else if (o1.getScore() < o2.getScore()) {

return -1;
        } else {
            return 0;
        }
    }

package tr
复制代码
复制代码
import java.util.TreeSet;
//测试类
public class Test {

    public static void main(String[] args) {
        TreeSet<Student> set = new TreeSet<Student>(new MyCmp());
        Student s1 = new Student("1001","chen",67);
        Student s2 = new Student("1001","zhang",17);
        Student s4 = new Student("1003","zeng",47);
        Student s5 = new Student("1004","wang",87);
        Student s6 = new Student("1005","chao",67);
        Student s3 = new Student("1002","zhou",62);
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);for(java.util.Iterator<Student> it= set.iterator();it.hasNext();){
            Student s = it.next();
            System.out.println(s.getSno()+","+s.getSname()+","+s.getScore());
        }
    }

}
复制代码

复制代码
/**
 * 学生类
 * @author Administrator
 *
 */
public class Student1 implements Comparable<Student1>{

    private String sno ;
    private String sname ;
    private Integer score ;
    
    public Student1(String sno, String sname, Integer score) {
        super();
        this.sno = sno;
        this.sname = sname;
        this.score = score;
    }
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public Integer getScore() {
        return score;
    }
    public void setScore(Integer score) {
        this.score = score;
    }
    
    // 自定义排序规则
    @Override
    public int compareTo(Student1 s) {
        if (this.getScore() > s.getScore()) {
            return 1;
        } else if (this.getScore() < s.getScore()) {
            return -1;
        } else {
            return 0;
        }
    }

}
复制代码

 

posted @   天葬  阅读(168)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示