TreeSet利用比较器排序

 需求:

        创建一个员工类Employee,有姓名name、年龄age、工资salary属性
    在TreeSet集合中,添加3个Employee对象
    要求:按照员工的工资排序;如果工资相同,按照年龄排序;如果也年龄相同,再按照姓名的字典顺序排序;
    (使用比较器排序)

Employee类:

package com.itheima.hashset.job.test02;

/**
 * @Auther: YeJunli
 * @Date: 2021/8/24 - 08 - 24 - 20:27
 * @Description:
 * @version: 1.0
 */
public class Employee {
    private String name;
    private int age;
    private int salary;

    public Employee() {
    }

    public Employee(String name, int age, int salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    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 int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Employee employee = (Employee) o;

        if (age != employee.age) return false;
        if (salary != employee.salary) return false;
        return name != null ? name.equals(employee.name) : employee.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        result = 31 * result + salary;
        return result;
    }

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

 测试类:

package com.itheima.hashset.job.test02;

import java.util.Collection;
import java.util.Comparator;
import java.util.TreeSet;

/**
 * @Auther: YeJunli
 * @Date: 2021/8/24 - 08 - 24 - 17:48
 * @Description:
 * @version: 1.0
 */
public class Test01 {
    //    第二题:创建一个员工类Employee,
//    有姓名name、年龄age、工资salary属性
//    在TreeSet集合中,添加3个Employee对象
//    要求:按照员工的工资排序;
//    如果工资相同,按照年龄排序;如果也年龄相同,
//    再按照姓名的字典顺序排序;
//            (使用比较器排序)
    public static void main(String[] args) {
        //调用getEmployees方法获取,一个TreeSet集合
        TreeSet<Employee> employees = getEmployees();
        Employee zs = new Employee("张三", 23, 3000);
        Employee lise = new Employee("李四", 25, 3500);
        Employee lishi = new Employee("李氏", 21, 3500);
        Employee anqi = new Employee("安琪", 21, 3500);
        Employee wangwu = new Employee("王五", 25, 4000);

        // 给集合添加 元素(对象)
        employees.add(zs);
        employees.add(lise);
        employees.add(lishi);
        employees.add(anqi);
        employees.add(wangwu);

        //
        for (Employee employee : employees) {
            System.out.println(employee);

        }
    }

    //比较器
    private static TreeSet<Employee> getEmployees() {
        return new TreeSet<>(new Comparator<Employee>() {
                @Override
                public int compare(Employee o1, Employee o2) {
                    int i = o1.getSalary() - o2.getSalary();
                    if (i==0){
                        i = o1.getAge() - o2.getAge();
                        if (i==0){
                            i = o1.getName().compareTo(o2.getName());
                        }
                    }
                    return i;
                }
            });
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值