Here is the code snippet with a compilation error:
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Employee> ss = List.of(
new Employee("Vitalii", "Hlushchenko"),
new Employee("Ruslan", "Ryapolov"));
List<Employee> lll = ss.stream()
.sorted(Comparator.comparing(e -> e.getName()).reversed())
.toList();
lll.forEach(System.out::println);
}
}
class Employee {
private String name;
private String surname;
public Employee(String name, String surname) {
this.name = name;
this.surname = surname;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public String toString() {
return name + " " + surname;
}
}
It shows compilation error in the following row:
.sorted(Comparator.comparing(e -> e.getName()).reversed())
The error is:
Cannot infer type argument(s) for <T, U> comparing(Function<? super T,? extends U>)
But if you remove the invokation of reversed() method the code is compiled sucessfully.
The question is: why the compilation error dissapearred. Is this a bug of a Java compiler?