Java 如何比较学生对象

Java 如何比较学生对象,java,Java,如何比较学生对象 import java.time.format.DateTimeFormatter; import java.util.Comparator; public class Student implements Comparable < Student >{ private String studentName; private String studentDOJ; public String getStudentName() {

如何比较学生对象

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

}
我的代码

如何比较学生对象 如何比较学生对象
如何比较学生对象 如何比较学生对象

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

}
导入java.time.format.DateTimeFormatter;
导入java.util.Comparator;
公共班级学生实施可比<学生>{
私有字符串studentName;
私家弦乐队;
公共字符串getStudentName(){
返回学生姓名;
}
public void setStudentName(字符串studentName){
this.studentName=studentName;
}
公共字符串getStudentDOJ(){
返回学生DOJ;
}
公共无效设置studentDOJ(字符串studentDOJ){
this.studentDOJ=studentDOJ;
}
@凌驾
公共国际比较(学生其他){
DateTimeFormatter formatter=模式的DateTimeFormatter.of(“dd-MM-yyyy”);
返回Comparator.comparing(LocalDate.parse(Student::getStudentDOJ,formatter)).reversed()
.比较(这个,其他);
}
}

此处不能使用方法引用

LocalDate.parse(CharSequence,DateTimeFormatter)
不使用
函数,因此无法传递
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);
}

这里不能使用方法引用

LocalDate.parse(CharSequence,DateTimeFormatter)
不使用
函数,因此无法传递
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);
}

您可以这样重写您的方法:

@Override
public int compareTo(Student other) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    return LocalDate.parse(getStudentDOJ(),formatter)
            .compareTo(LocalDate.parse(other.getStudentDOJ(),formatter));
}
并且可以将
list
类型的学生列表排序为:

lists.sort(Comparator.reverseOrder()); // dsc
lists.sort(Comparator.naturalOrder()); // asc

您可以这样重写您的方法:

@Override
public int compareTo(Student other) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    return LocalDate.parse(getStudentDOJ(),formatter)
            .compareTo(LocalDate.parse(other.getStudentDOJ(),formatter));
}
并且可以将
list
类型的学生列表排序为:

lists.sort(Comparator.reverseOrder()); // dsc
lists.sort(Comparator.naturalOrder()); // asc

studentDOJ
属性的类型是否为
String
?为什么不将其设置为
LocalDate
?这样可以避免在compareTo方法中解析字符串。是否有任何理由使
studentDOJ
属性的类型为
string
?为什么不将其设置为
LocalDate
?这样可以避免在compareTo方法中解析字符串。