java比较器comparator使用示例分享

所属分类: 软件编程 / java 阅读数: 87
收藏 0 赞 0 分享

复制代码 代码如下:

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

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多