java - 使用比较器接口对本地日期进行排序
问题描述
我正在尝试根据员工的加入日期对员工列表进行排序。下面是我的员工类。
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”。有人可以帮我吗?
解决方案
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);
}
});
推荐阅读
- python - 找不到满足需求请求的版本(来自版本:无)错误:未找到请求的匹配分发
- html - 如何使用 iframe 自动播放本地视频?
- wordpress - 将 URL 映射到文件系统
- discord - 无法使用 add_reaction discord.py(错误代码:10014):未知表情符号
- python - NumPy 安装问题
- ios - iMessage App - 在消息中显示等宽文本(蓝色气泡)
- php - 我只想返回 isactive = 1 的用户
- typescript - 如何在adonisJs v5中按关系列排序?
- loops - 如何使用 QTimer 命令
- php - 后台产品目录批发价格栏