java - Comparator.comparing(...) of a nested field -


suppose have domain model this:

class lecture {      course course;      ... // getters }  class course {      teacher teacher;      int studentsize;      ... // getters }  class teacher {      int age;      ... // getters } 

now can create teacher comparator this:

    return comparator             .comparing(teacher::getage); 

but how compare lecture's on nested fields, this?

    return comparator             .comparing(lecture::getcourse::getteacher:getage)              .thencomparing(lecture::getcourse::getstudentsize); 

i can't add method lecture.getteacherage() on model.

you can't nest method references. can use lambda expressions instead :

return comparator         .comparing(l->l.getcourse().getteacher().getage(), comparator.reverseorder())          .thencomparing(l->l.getcourse().getstudentsize()); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -