欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java比较器comparator使用示例分享

程序员文章站 2024-02-23 16:39:52
复制代码 代码如下:import java.util.arraylist;import java.util.collections;import java.util.com...

复制代码 代码如下:

import java.util.arraylist;
import java.util.collections;
import java.util.comparator;
import java.util.list;

public class comparatortest implements comparator<stuentity> {

    /**
     * @param args
     */
    public static void main(string[] args) {
        list<stuentity> list = new arraylist<stuentity>();
        stuentity stud1=new stuentity();
        stud1.setage(10);
        stud1.setname("abc");
        stuentity stud2=new stuentity();
        stud2.setage(10);
        stud2.setname("bdc");
        stuentity stud3=new stuentity();
        stud3.setage(5);
        stud3.setname("bdd");
        stuentity stud4=new stuentity();
        stud4.setage(30);
        stud4.setname("aad");

        list.add(stud1);
        list.add(stud2);
        list.add(stud3);
        list.add(stud4);

        collections.sort(list, new comparatortest());

        for(stuentity stud:list){
            system.out.println(stud.getage()+":"+stud.getname());
        }
    }
/**
 *
 */
    @override
    public int compare(stuentity stud1, stuentity stud2) {
        //根据姓名排序
        int maxname=stud1.getname().compareto(stud2.getname());
        if(maxname!=0)
            return maxname;
        //根据年龄排序
        int maxage=stud1.getage()-stud2.getage();
        //if(maxage!=0)
            return maxage;
    }
}

输出:

复制代码 代码如下:

30:aad
10:abc
10:bdc
5:bdd

java的比较器很有用,实现comparator接口的compare()这个回调方法来制定排序规则,然后调用collections.sort(list, new comparatortest());就可以将list进行排序,很方便

使用时要注意compare()方法中的return的先后顺序,优先的排序规则要写在前面

实体类:

复制代码 代码如下:

/**
 * 学生实体类
 *
 */
public class stuentity {
    private int studentid;// 学号
    private string name;
    private int age;
    private string sex;// 性别
    private int roomnumber;// 房间号
    private string degree;//学位
    private int grade;//年级
    private string devicenumber;// 设备号
    private int groupnumber;// 所属的小组
    private int javascore;// java成绩
    private int netscore;// net成绩
    public string getdegree() {
        return degree;
    }

    public void setdegree(string degree) {
        this.degree = degree;
    }

    public int getgrade() {
        return grade;
    }

    public void setgrade(int grade) {
        this.grade = grade;
    }

 

    /**
     * 初始化有参构造函数
     *
     * @param id
     * @param name
     * @param age
     * @param sex
     * @param roomnumber
     * @param devicenumber
     * @param groupnumber
     * @param javascore
     * @param netscore
     */
    public stuentity(string name, int age, string sex, int roomnumber,
            string devicenumber, int groupnumber, int javascore, int netscore) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.roomnumber = roomnumber;
        this.devicenumber = devicenumber;
        this.groupnumber = groupnumber;
        this.javascore = javascore;
        this.netscore = netscore;
    }

    /**
     * 无参构造函数
     */
    public stuentity() {

    }

    public int getjavascore() {
        return javascore;
    }

    public void setjavascore(int javascore) {
        this.javascore = javascore;
    }

    public int getnetscore() {
        return netscore;
    }

    public void setnetscore(int netscore) {
        this.netscore = netscore;
    }

   

    public int getstudentid() {
        return studentid;
    }

    public void setstudentid(int studentid) {
        this.studentid = studentid;
    }

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }

    public int getage() {
        return age;
    }

    public void setage(int age) {
        this.age = age;
    }

    public string getsex() {
        return sex;
    }

    public void setsex(string sex) {
        this.sex = sex;
    }

    public int getroomnumber() {
        return roomnumber;
    }

    public void setroomnumber(int roomnumber) {
        this.roomnumber = roomnumber;
    }

    public string getdevicenumber() {
        return devicenumber;
    }

    public void setdevicenumber(string devicenumber) {
        this.devicenumber = devicenumber;
    }

    public int getgroupnumber() {
        return groupnumber;
    }

    public void setgroupnumber(int groupnumber) {
        this.groupnumber = groupnumber;
    }
}