gpt4 book ai didi

java - 使用比较器接口(interface)对本地日期进行排序

转载 作者:行者123 更新时间:2023-12-02 00:24:46 24 4
gpt4 key购买 nike

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

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

}

下面是我的比较器类:

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,运算符 > 未定义”。有人可以帮我解决这个问题吗?

最佳答案

dateOfJoiningLocalDate您无法通过 > 进行比较, <== 。 Java 仅支持基元上的数学符号(及其对象表示)。相反,您可以使用方法 .isBefore().isAfter() ,参见here .

示例:

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

});

关于java - 使用比较器接口(interface)对本地日期进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58058958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com