Java基础进阶集合-Comparable接口,Comparator比较器案例

简介: Java基础进阶集合-Comparable接口,Comparator比较器案例

定义一个Employee类


该类包括:


private 成员变量 name ,age,birthday,其中birthday为MyDate类的对象


并为每一个属性定义getter setter方法


定义Mydate类包含:


private 成员变量 year,month,day 并为每一个属性定义getter setter方法

创建该Employee类的5个对象,并把这些对象放入TreeSet集合中,分别按以下两种方式对集合中的元素进行排序,并遍历输出


(1)使Employee实现Comparable接口,并按name排序,字典表顺序


(2)创建TreeSet时传入Comparator对象,按生日日期的对员工进行先后顺序排序


1992 - 05 - 12


1992 - 05 - 18


1992 - 07 - 15


1993 - 06 - 15


1998 - 02 - 03


①Emoplyee.java


public class Employee implements Comparable<Employee>{
    private String name;;
    private int age;
    private MyDate birthday;
    public Employee(){}
    public Employee(int age){
        this.age = age;
    }
    public Employee(String name){this.name = name;}
    public Employee(String name, int age, MyDate birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }
    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 MyDate getBirthday() {
        return birthday;
    }
    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }
    @Override
    public int compareTo(Employee e) {
        if(this.getAge() == e.getAge()) {
            return this.getName().compareTo(e.getName());
        }else{
            return this.getAge() - e.getAge();
        }
    }
    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}


②MyDate.java


public class MyDate {
    private int year;
    private int month;
    private int day;
    public MyDate(){
    }
    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 +
                '}';
    }
}


③Test.java


public class Test {
    public static void main(String[] args) {
        //创建TreeSet集合对象
        TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {//比较器比较生日
            @Override
            public int compare(Employee o1, Employee o2) {
                int year = o1.getBirthday().getYear();
                int month = o1.getBirthday().getMonth();
                int day = o1.getBirthday().getDay();
                int year1 = o2.getBirthday().getYear();
                int month1 = o2.getBirthday().getMonth();
                int day1 = o2.getBirthday().getDay();
                int result = year - year1;
                int result2 = result == 0  ? month - month1 : result ;
                int result3 = result2 ==0 ? day - day1 : result2;
                return result3;
            }
        });
        //创建Empployee对象
        Employee e1 = new Employee("zhangsan",20,new MyDate(1992,05,12));
        Employee e2 = new Employee("lisi",18,new MyDate(1992,05,18));
        Employee e3 = new Employee("wangwu",18,new MyDate(1992,07,15));
        Employee e4 = new Employee("zhaoliu",13,new MyDate(1992,05,12));
        Employee e5 = new Employee("xuqi",80,new MyDate(1993,06,15));
        //把Employee对象放到TreeSet集合中
        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);
        //遍历
        for(Employee e : set){
            System.out.println(e);
        }
    }
}


测试结果:


0a2653c851af460fa595bd959398a8f1.png

目录
打赏
0
0
0
0
4
分享
相关文章
JAVA接入DeepSeek大模型接口开发---阿里云的百炼模型
随着大模型的越来越盛行,现在很多企业开始接入大模型的接口,今天我从java开发角度来写一个demo的示例,用于接入DeepSeek大模型,国内的大模型有很多的接入渠道,今天主要介绍下阿里云的百炼模型,因为这个模型是免费的,只要注册一个账户,就会免费送百万的token进行学习,今天就从一个简单的可以执行的示例开始进行介绍,希望可以分享给各位正在学习的同学们。
109 3
JAVA接入DeepSeek大模型接口开发---阿里云的百炼模型
java语言后台管理若依框架-登录提示404-接口异常-系统接口404异常如何处理-登录验证码不显示prod-api/captchaImage 404 (Not Found) 如何处理-解决方案优雅草卓伊凡
java语言后台管理若依框架-登录提示404-接口异常-系统接口404异常如何处理-登录验证码不显示prod-api/captchaImage 404 (Not Found) 如何处理-解决方案优雅草卓伊凡
230 5
Java 排序神器:Comparable 和 Comparator 该怎么选?
嗨,大家好,我是小米!今天和大家聊一聊Java社招面试中常考的经典问题——Comparable和Comparator的区别。Comparable定义对象的自然排序,适用于单一固定的排序规则;Comparator则是策略接口,用于定义自定义排序规则,适用于多样化或多变的排序需求。掌握这两者的区别是理解Java排序机制的基础,也是面试中的加分题。结合实际项目场景深入探讨它们的应用,能更好地打动面试官。如果你觉得有帮助,欢迎点赞、收藏、分享,期待你的一键三连!我们下期见~ 我是小米,一个喜欢分享技术的程序员,关注我的微信公众号“软件求生”,获取更多技术干货!
54 20
利用Java获取京东SKU接口指南
本文介绍如何使用Java通过京东API获取商品SKU信息。首先,需注册京东开放平台账号并创建应用以获取AppKey和AppSecret。接着,查阅API文档了解调用方法。明确商品ID后,构建请求参数并通过HTTP客户端发送请求。最后,解析返回的JSON数据提取SKU信息。注意遵守API调用频率限制及数据保护法规。此方法适用于电商平台及其他数据获取场景。
|
3月前
|
java如何请求接口然后终止某个线程
通过本文的介绍,您应该能够理解如何在Java中请求接口并根据返回结果终止某个线程。合理使用标志位或 `interrupt`方法可以确保线程的安全终止,而处理好网络请求中的各种异常情况,可以提高程序的稳定性和可靠性。
72 6
【Java】Comparable和Comparator接口
【Java】Comparable和Comparator接口
511 0
【Java】Comparable和Comparator接口
深度解析Java中的Comparable接口和Comparator接口
深度解析Java中的Comparable接口和Comparator接口
158 0
【错误记录】Java 中 ArrayList 排序 ( 使用 Comparator 接口时注意 compare 返回值是 -1 和 +1 )
【错误记录】Java 中 ArrayList 排序 ( 使用 Comparator 接口时注意 compare 返回值是 -1 和 +1 )
209 0
【错误记录】Java 中 ArrayList 排序 ( 使用 Comparator 接口时注意 compare 返回值是 -1 和 +1 )
JAVA Comparator接口和Comparator接口
JAVA Comparator接口和Comparator接口
158 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等