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

Posted bigsiji

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
        }
    }

}

 技术分享图片




以上是关于Comparator与Comparable,自定义排序和类比较器,TreeSet对象排序的主要内容,如果未能解决你的问题,请参考以下文章

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

Comparable接口与Comparator接口的区别

comparator接口与Comparable接口的区别

comparator接口与Comparable接口的区别

comparator接口与Comparable接口的区别

comparable接口 和 comparator接口的特点与区别