如何使用java 8最小化此Comparator方法 实际上,我传递了包含日期列表的工作日列表,我需要按照升序和降序对日期进行排序 我需要按升序排列日期
private List<WorkingDays> workingDaysDateSortDesc(List<WorkingDays> workingDays) {
Collections.sort(workingDays.get(0).getDays(), new Comparator<Days>() {
@Override
public int compare(Days o1, Days o2) {
if (o1.getDate().get$date() == null || o2.getDate().get$date() == null)
return 0;
return o2.getDate().get$date().compareTo(o1.getDate().get$date());
}
});
return workingDays;
}
答案 0 :(得分:0)
只需使用lambda:
private List<WorkingDays> workingDaysDateSortDesc(List<WorkingDays> workingDays) {
Collections.sort(workingDays.get(0).getDays(), (Comparator<Days>) (o1, o2) -> {
if (o1.getDate().get$date() == null || o2.getDate().get$date() == null)
return 0;
return o2.getDate().get$date().compareTo(o1.getDate().get$date());
});
return workingDays;
}