简体   繁体   English

使用比较器接口对本地日期进行排序

[英]Using comparator interface to sort localdate

I am trying to sort a list of employees based on their date of joining.我正在尝试根据员工的加入日期对员工列表进行排序。 Below is my employee class.下面是我的员工 class。

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 + "]";
    }

}

And below is my Comparator class:下面是我的比较器 class:

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

        });

But while trying to sort the list of employees based on their DOJ, I am getting error "The operator > is undefined for the argument type(s) java.time.LocalDate, java.time.LocalDate".但是,在尝试根据 DOJ 对员工列表进行排序时,我收到错误消息“操作员 > 未定义参数类型 java.time.LocalDate、java.time.LocalDate 的参数类型”。 Can someone please help me on this?有人可以帮我吗?

dateOfJoining is a LocalDate which you cannot compare by > , < or == . dateOfJoining是一个LocalDate ,您无法通过><==进行比较。 Java only supports those mathematical symbols on primitives (and their object representations). Java 仅支持原语上的那些数学符号(及其 object 表示)。 Instead you can use the methods .isBefore() or .isAfter() , see here .相反,您可以使用.isBefore().isAfter()方法,请参见此处

Example:例子:

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

        });

Alternatively:或者:

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

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

        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM