5
假設我有一個領域模型是這樣的:Comparator.comparing(...)嵌套場
class Lecture {
Course course;
... // getters
}
class Course {
Teacher teacher;
int studentSize;
... // getters
}
class Teacher {
int age;
... // getters
}
現在,我可以創造一個老師比較喜歡這樣的:
return Comparator
.comparing(Teacher::getAge);
但如何我會比較一下嵌套字段的講座嗎?
return Comparator
.comparing(Lecture::getCourse::getTeacher:getAge)
.thenComparing(Lecture::getCourse::getStudentSize);
我不能在模型上添加一個方法Lecture.getTeacherAge()
。
爲什麼不使用lambda? – njzk2
啊......那一刻,當我意識到我問了一個愚蠢的問題:)(不是說有什麼愚蠢的問題。) –