首页 > 解决方案 > 使用比较器接口对本地日期进行排序

问题描述

我正在尝试根据员工的加入日期对员工列表进行排序。下面是我的员工类。

import java.time.LocalDate;

public class Employee {

    private String name;
    private String empID;
    private Designation designation;
    private LocalDate dateOfJoining;
    private int monthlySalary;

    public Employee(String name, String empID, Designation designation, LocalDate dateOfJoining, int monthlySalary) {
        super();
        this.name = name;
        this.empID = empID;
        this.designation = designation;
        this.dateOfJoining = dateOfJoining;
        this.monthlySalary = monthlySalary;
    }

    public String getName() {
        return name;
    }

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

    public String getEmpID() {
        return empID;
    }

    public void setEmpID(String empID) {
        this.empID = empID;
    }

    public Designation getDesignation() {
        return designation;
    }

    public void setDesignation(Designation designation) {
        this.designation = designation;
    }

    public LocalDate getDOJ() {
        return dateOfJoining;
    }

    public void setDOJ(LocalDate dOJ) {
        dateOfJoining = dOJ;
    }

    public int getMonthlySalary() {
        return monthlySalary;
    }

    public void setMonthlySalary(int monthlySalary) {
        this.monthlySalary = monthlySalary;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((dateOfJoining == null) ? 0 : dateOfJoining.hashCode());
        result = prime * result + ((designation == null) ? 0 : designation.hashCode());
        result = prime * result + ((empID == null) ? 0 : empID.hashCode());
        result = prime * result + monthlySalary;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (dateOfJoining == null) {
            if (other.dateOfJoining != null)
                return false;
        } else if (!dateOfJoining.equals(other.dateOfJoining))
            return false;
        if (designation == null) {
            if (other.designation != null)
                return false;
        } else if (!designation.equals(other.designation))
            return false;
        if (empID == null) {
            if (other.empID != null)
                return false;
        } else if (!empID.equals(other.empID))
            return false;
        if (monthlySalary != other.monthlySalary)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", empID=" + empID + ", designation=" + designation + ", DOJ=" + dateOfJoining
                + ", monthlySalary=" + monthlySalary + "]";
    }

}

下面是我的 Comparator 类:

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Employecomparable {

    public static void main(String[] args) {
        List<Employee> listofemployee = new ArrayList<>();

        LocalDate date1 = LocalDate.parse("2018-09-06");
        LocalDate date2= LocalDate.of(2011, 12, 12);
        listofemployee.add(new Employee("abc", "12345", Designation.ASE,LocalDate.of(2014, 2, 15) , 20000));
        listofemployee.add(new Employee("abcd", "24680", Designation.SE, date1, 30000));
        listofemployee.add(new Employee("abcde", "13570", Designation.SSE, LocalDate.of(2013, 05, 30), 40000));
        listofemployee.add(new Employee("abcdef", "13690", Designation.TL, date2, 60000));
        listofemployee.add(new Employee("xyz", "10909", Designation.AM, LocalDate.parse("20/03/2000"), 800000));
        listofemployee.add(new Employee("koool", "89076", Designation.M, LocalDate.parse("31/01/2011"), 2000));

        Collections.sort(listofemployee, new Comparator<Employee>() {

            @Override
            public int compare(Employee emp1, Employee emp2) {
                if (emp1.getMonthlySalary() > emp2.getMonthlySalary())
                    return -1;
                else if (emp1.getMonthlySalary() < emp2.getMonthlySalary())
                    return +1;
                else
                    return 0;
            }

        });
        System.out.println(listofemployee);
        Collections.sort(listofemployee, new Comparator<Employee>(){

            @Override
            public int compare(Employee emp1, Employee emp2) {
                if(emp1.getDOJ()>emp2.getDOJ())
                    return +1;

                return 0;
            }

        });

但是,在尝试根据 DOJ 对员工列表进行排序时,我收到错误“操作符 > 未定义参数类型 java.time.LocalDate、java.time.LocalDate”。有人可以帮我吗?

标签: javaarraylistcollections

解决方案


dateOfJoining是一个LocalDate你不能用>,<或比较的==。Java 仅支持原语(及其对象表示)上的那些数学符号。相反,您可以使用方法.isBefore().isAfter(),请参阅此处

例子:

     Collections.sort(listofemployee, new Comparator<Employee>(){

            @Override
            public int compare(Employee emp1, Employee emp2) {
                if(emp1.getDOJ().isBefore(emp2.getDOJ()))
                    return +1;
                else if (emp1.getDOJ().isAfter(emp2.getDOJ)))
                    return -1
                else 
                    return 0;
            }

        });

或者:

 Collections.sort(listofemployee, new Comparator<Employee>(){

            @Override
            public int compare(Employee emp1, Employee emp2) {
                return emp1.getDOJ().compareTo(emp2.getDOJ);
            }

        });

推荐阅读