java比较器comparator使用示例分享

时间:2022-07-18 22:45:15 


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;
    }
}

标签:java,比较器,comparator
0
投稿

猜你喜欢

  • SpringBoot配置文件方式,在线yml文件转properties

    2023-02-25 08:43:54
  • Java 抽象类特点总结

    2023-07-28 10:39:46
  • IDEA-Maven项目的jdk版本设置方法

    2022-07-21 16:04:17
  • C# 用什么方法将BitConverter.ToString产生字符串再转换回去

    2021-10-07 17:49:40
  • Android实现表情功能

    2023-03-05 06:46:27
  • Android拼接实现动态对象方法详解

    2021-12-23 10:09:46
  • Android利用BitMap获得图片像素数据的方法

    2023-06-19 15:50:47
  • 浅析Disruptor高性能线程消息传递并发框架

    2023-02-26 14:09:01
  • Android自定义控件之圆形、圆角ImageView

    2023-04-16 21:55:11
  • Spring实战之协调作用域不同步的Bean操作示例

    2023-01-23 02:48:10
  • java递归算法的实例详解

    2021-06-07 12:15:17
  • Java详解IO流创建读取与写入操作

    2023-01-23 01:50:28
  • Java中的递归方法示例介绍

    2023-07-20 18:04:11
  • Android ImgView属性图文详解

    2023-02-21 11:14:18
  • Java面试题冲刺第二十四天--并发编程

    2023-08-31 05:39:02
  • Intellij Idea修改代码方法参数自动提示快捷键的操作

    2022-11-19 08:08:37
  • C#异步编程由浅入深(二)之Async/Await的使用

    2022-10-14 16:45:20
  • java 同步、异步、阻塞和非阻塞分析

    2022-08-09 03:02:42
  • Java Valhalla Project项目介绍

    2021-10-03 00:29:28
  • Android 大文件切割与合并的实现代码

    2023-05-25 21:58:58
  • asp之家 软件编程 m.aspxhome.com