泛型的使用案例,以及年月日的定制排序,传入Comparator对象

news2024/10/7 18:24:47

 

简易版:传送门:过关展将之——birthday排序(年月日排序)-CSDN博客  

 实现过程;

public static void main(String[] args) {
        ArrayList<Employee> arrayList = new ArrayList<>();
        Employee grace = new Employee("Grace", 12000, new MyDate(2012, 12, 12));
        Employee tom = new Employee("Tom", 28000, new MyDate(2001, 12, 03));
        Employee david = new Employee("David", 30000, new MyDate(1999, 10, 10));
        Employee david1 = new Employee("David", 31000, new MyDate(2001, 11, 15));
        Employee david2 = new Employee("David", 32000, new MyDate(1899, 5, 14));
        Employee david3 = new Employee("David", 33000, new MyDate(2013, 04, 11));
        Employee david4 = new Employee("David", 34000, new MyDate(1987, 04, 10));
        Employee david5 = new Employee("David", 34000, new MyDate(1987, 04, 11));
        arrayList.add(grace);
        arrayList.add(tom);
        arrayList.add(david);
        arrayList.add(david1);
        arrayList.add(david2);
        arrayList.add(david3);
        arrayList.add(david4);
        arrayList.add(david5);
//        System.out.println("打印先前数据:" + arrayList);
        //现在进行排序
        arrayList.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                if (!(o1.getName() != null && o2.getName() != null)) {
                    throw new RuntimeException("员工姓名不能为空!");
                }else{
                    if (o1.getName().equals(o2.getName())) {
                        if (o1.getBirthday().getYear() == o2.getBirthday().getYear()) {
                            if (o1.getBirthday().getMonth() == o2.getBirthday().getMonth()) {
                                if (o1.getBirthday().getDay() == o2.getBirthday().getDay()) {
                                    return 0;
                                } else if (o1.getBirthday().getDay() > o2.getBirthday().getDay()) {
                                    return 1;
                                } else {
                                    return -1;
                                }
                            } else if (o1.getBirthday().getMonth() > o2.getBirthday().getMonth()) {
                                return 1;
                            } else {
                                return -1;
                            }
                        } else if (o1.getBirthday().getYear() > o2.getBirthday().getYear()) {
                            return 1;
                        } else {
                            return -1;
                        }
                    } else {
                        return o1.getName().compareTo(o2.getName());
                    }
                }
            }
        });
        System.out.println("========排序后======");
        for (Object o :arrayList) {
            Employee employee = (Employee) o;
            System.out.println(employee);
        }

    }
}

class MyDate {
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}

class Employee {

    private String name;

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    private double sal;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }
}

 以上代码过于繁琐,使用 if 的嵌套,可读性不高,现在进行改良:

进一步优化过程:

 @SuppressWarnings({"all"})
    public static void main(String[] args) {
        ArrayList<EmployeeImp> arrayList = new ArrayList<>();
        EmployeeImp grace = new EmployeeImp("Grace", 12000, new MyDateImp(2012, 12, 12));
        EmployeeImp tom = new EmployeeImp("Tom", 28000, new MyDateImp(2001, 12, 03));
        EmployeeImp david = new EmployeeImp("David", 30000, new MyDateImp(1999, 10, 10));
        EmployeeImp david1 = new EmployeeImp("David", 31000, new MyDateImp(2001, 11, 15));
        EmployeeImp david2 = new EmployeeImp("David", 32000, new MyDateImp(1899, 5, 14));
        EmployeeImp david3 = new EmployeeImp("David", 33000, new MyDateImp(2013, 04, 11));
        EmployeeImp david4 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 10));
        EmployeeImp david5 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 11));
        arrayList.add(grace);
        arrayList.add(tom);
        arrayList.add(david);
        arrayList.add(david1);
        arrayList.add(david2);
        arrayList.add(david3);
        arrayList.add(david4);
        arrayList.add(david5);

        //下面开始订制排序
        arrayList.sort(new Comparator<EmployeeImp>() {
            @Override
            public int compare(EmployeeImp o1, EmployeeImp o2) {
                if (!(o1 instanceof EmployeeImp && o2 instanceof EmployeeImp)) {
                    System.out.println("类型不匹配");
                    return 0;
                }
                //比较name
                int i = o1.getName().compareTo(o2.getName());
                if (i==0) {//如果名字相同,就继续比较
                    int yearMinus =o1.getBirthday().getYear() - o2.getBirthday().getYear();
                    if(yearMinus==0){//如果两个年份相同,继续比较月份
                        int monthMinus = o1.getBirthday().getMonth()- o2.getBirthday().getMonth();
                        if(monthMinus==0){//如果两个月份相同,继续比较日
                            int dayMinus = o1.getBirthday().getDay()- o2.getBirthday().getDay();
                            if(dayMinus==0){
                                return 0;
                            }else{
                                return dayMinus;
                            }
                        }else{
                            return monthMinus;
                        }
                    }else{
                        return yearMinus;
                    }

                } else{//如果名字不相同,就返回,出结果
                    return i;
                }
            }
        });
        System.out.println("======定制排序后=====");
        for (Object o :arrayList) {
            EmployeeImp employeeImp = (EmployeeImp) o;
            System.out.println(employeeImp);
        }
    }
}

class MyDateImp {
    private int year;
    private int month;
    private int day;

    public MyDateImp(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}

class EmployeeImp {

    private String name;

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    private double sal;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDateImp getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDateImp birthday) {
        this.birthday = birthday;
    }

    private MyDateImp birthday;

    public EmployeeImp(String name, double sal, MyDateImp birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }
}

使用过关斩将法改良:

   public static void main(String[] args) {
        ArrayList<EmployeeImp> arrayList = new ArrayList<>();
        EmployeeImp grace = new EmployeeImp("Grace", 12000, new MyDateImp(2012, 12, 12));
        EmployeeImp tom = new EmployeeImp("Tom", 28000, new MyDateImp(2001, 12, 03));
        EmployeeImp david = new EmployeeImp("David", 30000, new MyDateImp(1999, 10, 10));
        EmployeeImp david1 = new EmployeeImp("David", 31000, new MyDateImp(2001, 11, 15));
        EmployeeImp david2 = new EmployeeImp("David", 32000, new MyDateImp(1899, 5, 14));
        EmployeeImp david3 = new EmployeeImp("David", 33000, new MyDateImp(2013, 04, 11));
        EmployeeImp david4 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 10));
        EmployeeImp david5 = new EmployeeImp("David", 34000, new MyDateImp(1987, 04, 11));
        arrayList.add(grace);
        arrayList.add(tom);
        arrayList.add(david);
        arrayList.add(david1);
        arrayList.add(david2);
        arrayList.add(david3);
        arrayList.add(david4);
        arrayList.add(david5);

        //下面开始订制排序
        arrayList.sort(new Comparator<EmployeeImp>() {
            @Override
            public int compare(EmployeeImp o1, EmployeeImp o2) {
                if (!(o1 instanceof EmployeeImp && o2 instanceof EmployeeImp)) {
                    System.out.println("类型不匹配");
                    return 0;
                }
                //比较name
                int i = o1.getName().compareTo(o2.getName());
                if (i != 0) {//如果名字不相同,直接返回
                    return i;
                }
                //以下是对birthday的比较,因此,我们最好把这个比较,放在MyDate
                int yearMinus = o1.getBirthday().getYear() - o2.getBirthday().getYear();
                if (yearMinus != 0) {//如果两个年份不相同,直接返回
                    return yearMinus;
                }
                int monthMinus = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
                if (monthMinus != 0) {//如果两个月份不相同,直接返回
                    return monthMinus;
                }
                //如果year、month都相同
                    return o1.getBirthday().getDay() - o2.getBirthday().getDay();
            }
        });
        System.out.println("======定制排序后=====");
        for (
                Object o : arrayList) {
            EmployeeImp employeeImp = (EmployeeImp) o;
            System.out.println(employeeImp);
        }
    }
}

class MyDateImp {
    private int year;
    private int month;
    private int day;

    public MyDateImp(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}

class EmployeeImp {

    private String name;

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    private double sal;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDateImp getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDateImp birthday) {
        this.birthday = birthday;
    }

    private MyDateImp birthday;

    public EmployeeImp(String name, double sal, MyDateImp birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }
}

 

打印结果:

简易版:传送门:过关展将之——birthday排序(年月日排序)-CSDN博客 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1123852.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

面对“双十一”这样的大促,品牌方还能多做些什么?

小编叨叨&#xff1a;当前市场环境不佳&#xff0c;经济下行时用户对于商品价格最敏感&#xff0c;当然价格也是决定企业经营利润&#xff0c;市场竞争力及用户口碑的重要核心影响因素&#xff0c;所以我们特地为企业准备了商品智能定价咨询及数据运营咨询服务&#xff0c;有需…

英语——分享篇——每日200词——3201-3400

3201——air-conditioning——[eərkəndɪʃnɪŋ]——n.空调设备&#xff1b;vt.给…装上空调——air-conditioning——air-condition空调(熟词)ing鹰(谐音)——空调设备的噪音让鹰不得安宁——The trains dont even have proper air-conditioning, grumbles Mr So. ——地铁…

深度学习_5_模型拟合_梯度下降原理

需求: 想要找到一条直线&#xff0c;能更好的拟合这一些点 如何确定上述直线就是最优解呢&#xff1f; 由计算机算出所有点与我们拟合直线的误差&#xff0c;常见的是均方误差 例如&#xff1a;P1与直线之间的误差为e1 将P1坐标带入直线并求误差得&#xff1a; 推广到所有点&a…

买充电桩你必须要知道三件事

分享一套开源充电桩云平台&#xff08;v2.5.1&#xff09;-- 支持二轮(电动自行车)、四轮(电动汽车) 中国电动汽车充电基础设施促进联盟的信息部主任&#xff0c;仝宗旗&#xff0c;在2022年接受了《中国汽车报》采访的时候&#xff0c;提到过一个数据。 电动车主在小区成功安…

计算机考研自命题(5)

1、C语言–求和 1、展开式求和。输入一个实数x&#xff0c;计算并输出下式的和&#xff0c;直到最后一项的绝对值小于0.00001.计算结果保留2位小数&#xff0c;试编程。 S x x/2&#xff01; x/3&#xff01; … /* 算法思想&#xff1a;定义一个求阶乘的函数fact(), 头文件调…

未能为 SSL/TLS 安全通道建立信任关系

在 Windows早期版本&#xff08;Windows server 2008&#xff09;上运行web请求相关代码&#xff0c;提示错误&#xff1a;未能为 SSL/TLS 安全通道建立信任关系。 打开IE直接访问相关网址&#xff0c;按照提示信任网站&#xff0c;安装证书&#xff1a; 选择&#xff1a;将所有…

【IO】JavaIO流:字节流、字符流、缓冲流、转换流、序列化流等

个人简介&#xff1a;Java领域新星创作者&#xff1b;阿里云技术博主、星级博主、专家博主&#xff1b;正在Java学习的路上摸爬滚打&#xff0c;记录学习的过程~ 个人主页&#xff1a;.29.的博客 学习社区&#xff1a;进去逛一逛~ IO流 Java IO1. 认识IO2. FileOutputStream(写…

基于springboot+vue网吧管理系统52

大家好✌&#xff01;我是CZ淡陌。一名专注以理论为基础实战为主的技术博主&#xff0c;将再这里为大家分享优质的实战项目&#xff0c;本人在Java毕业设计领域有多年的经验&#xff0c;陆续会更新更多优质的Java实战项目&#xff0c;希望你能有所收获&#xff0c;少走一些弯路…

FFMPEG之example编译

FFMPEG源码下载:Download FFmpeg 编译需配置的库: sudo apt-get install yasm sudo apt-get install libsdl1.2-dev sudo apt-get install libsdl2-dev 编译流程: ./configure --disable-x86asm --prefix=路径 --enable-shared 按照提示添加 --dis…

全志R128将LVGL运行在SPI TFT GUI上

LVGL 与 SPI TFT GUI 本次使用的是 Dshan_Display Module&#xff0c;如下图&#xff1a; 引脚配置如下&#xff1a; R128 DevkitTFT 模块PA12CSPA13SCKPA18MOSIPA9PWMPA20RESETPA19RS3V33.3VGNDGND 载入方案 我们使用的开发板是 R128-Devkit&#xff0c;需要开发 C906 核心…

【网络安全 --- xss-labs靶场通关(11-20关)】详细的xss-labs靶场通关思路及技巧讲解,让你对xss漏洞的理解更深刻

如果需要安装各种系统&#xff0c;虚拟机&#xff0c;工具等等关注我&#xff0c;已经在出系统的课程了 一&#xff0c;靶场安装 超详细的靶场安装教程如下&#xff0c;提供工具&#xff0c;靶场&#xff0c;镜像等 【网络安全 --- xss-labs靶场】xss-labs靶场安装详细教程&…

uniapp canvas 无法获取 webgl context 的问题解决

uniapp canvas 无法获取 webgl context 的问题解决 一、问题描述 在 uniapp 中做一个查看监控视频的页面&#xff0c;用到的是 JSMpeg 这个库&#xff0c;原理就是前后台通过 websocket 不断推送新画面内容到前端&#xff0c;前端通过这个 JSMpeg 渲染到前端页面中指定的 can…

selenium4 元素定位

selenium4 9种元素定位 ID driver.find_element(By.ID,"kw")NAME driver.find_element(By.NAME,"tj_settingicon")CLASS_NAME driver.find_element(By.CLASS_NAME,"ipt_rec")TAG_NAME driver.find_element(By.TAG_NAME,"area")LINK_T…

10.22A*算法,华容道,状态压缩

A* P1379华容道 问题是要从初始布局用最少的步骤到目标布局&#xff0c;每次只能动0周围的格子&#xff0c;就是华容道&#xff1b; 怎么用算法的思路解决&#xff1f; 状态压缩思路 每个数字就代表当前的状态&#xff0c;队列和map函数都记录的是当前的状态数&#xff0c;…

众和策略:跨界收购算力公司 高新发展斩获3连板

高新展开23日开盘再度一字涨停&#xff0c;截至发稿&#xff0c;该股报21.74元&#xff0c;涨停板上封单超200万手。至此&#xff0c;该股已连续3个生意日涨停。 公司18日晚间宣布生意预案&#xff0c;拟通过发行股份及支付现金的方式购买高投电子集团持有的华鲲振宇30%股权、…

P1451 求细胞数量 题解

文章目录 题目描述输入格式输出格式样例样例输入样例输出 数据范围与提示思路及部分实现完整代码 题目描述 一矩形阵列由数字 0 0 0 到 9 9 9 组成&#xff0c;数字 1 1 1 到 9 9 9 代表细胞&#xff0c;细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞&#x…

10.23归并排序

课上 归并排序 最大时&#xff0c;就是两个都是完全倒序&#xff0c;但注意一定有一个序列先用完&#xff0c;此时剩一个序列只有一个元素&#xff0c;不用比较&#xff0c;直接加入&#xff0c;所以就是nn-1, 最小时&#xff0c;是都是完全有序&#xff0c;且一个序列中的元…

ZYNQ移植ARM CMSIS_DSP库

移植方法 Vitis中新建一个Application Project&#xff0c;选择HelloWord模板。按下面步骤移植CMSIS_DSP&#xff1a; 下载CMSIS_DSP&#xff0c;拷贝如下文件夹到Vitis工程&#xff1a; SourceIncludePrivateIncludeComputeLibrary (only if you target Neon) 对Source路径下…

古铜色的P1.2直径1.2米无边圆形(饼)创意LED显示屏绽放上海苏河湾万象天地展厅

我们公司的圆形LED显示屏是专门根据业主的独特需求开模定制的。为了满足各种不同的规格和要求&#xff0c;我们已经成功地对多个点间距与直径进行了精密的开模设计&#xff0c;且工艺技术成熟稳定&#xff0c;能够保证快速的交期。 这种圆形LED显示屏是通过独特的开模定制方式…

基础课6——计算机视觉

1.计算机视觉的概念与原理 1.1概念 计算机视觉&#xff08;CV&#xff09;是人工智能的一个重要发展领域&#xff0c;属于计算机科学的一个分支&#xff0c;它企图让计算机能像人类一样通过视觉来获取和理解信息。计算机视觉的应用非常广泛&#xff0c;包括但不限于图像识别、…