码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-09-05 20:01:27      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:super   return   imp   ycm   学生   inf   比较   generate   student   

/**
 * 学生类
 * @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对象排序

标签:super   return   imp   ycm   学生   inf   比较   generate   student   

原文地址:https://www.cnblogs.com/bigsiji/p/9591506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!