Can anyone please help me understand why does the below method reference works with thenComparing method:
List<Person> li = personList.stream()
.sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getName))
.collect(Collectors.toList());
But when I am trying to do it using a lambda expression, it doesn't work. Here, I am getting a compilation error - "Cannot resolve method getAge in 'Object'" :
List<Person> li = personList.stream()
.sorted(Comparator.comparing(person -> person.getAge()).thenComparing(person -> person.getName()))
.collect(Collectors.toList());
Moreover, I can see that if I remove the thenComparing method, then this code again works with lambda expression:
List<Person> li = personList.stream()
.sorted(Comparator.comparing(person -> person.getAge()))
.collect(Collectors.toList());
Can you please let me know where I am going wrong?