如何比较学生对象
我的代码
如何比较学生对象
如何比较学生对象
如何比较学生对象
如何比较学生对象
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
public class Student implements Comparable < Student >{
private String studentName;
private String studentDOJ;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentDOJ() {
return studentDOJ;
}
public void setStudentDOJ(String studentDOJ) {
this.studentDOJ = studentDOJ;
}
@Override
public int compareTo(Student other) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
return Comparator.comparing(LocalDate.parse(Student::getStudentDOJ,formatter)).reversed()
.compare(this, other);
}
}
最佳答案
您不能在那里使用方法参考。LocalDate.parse(CharSequence, DateTimeFormatter)
不使用Function<Student, String>
,因此您不能通过Student::getStudentDOJ
。
@Override
public int compareTo(Student other) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
return Comparator.comparing((Student student) -> LocalDate.parse(student.getStudentDOJ(), formatter)).reversed()
.compare(this, other);
}
关于java - 如何比较学生对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57708455/