I have a simple java class with some variables String, Double, Integer etc.
public class MyClass {
private String name;
private String status;
private Double risk;
private Double health;
// more variables with all getters and setters
}
I am trying to write a generic method which will return the Function<T, U>
type. I will further use this function for sorting. It is throwing compile time error
The type of getName() from the type MyClass is String, this is incompatible with the descriptor's return type: U
at line func = MyClass::getName;
and error
The type of getRisk() from the type MyClass is Double, this is incompatible with the descriptor's return type: U
at line func = MyClass::getRisk;
private static <T extends MyClass, U extends Comparable<U>> Function<T, U> getSortFunction(final String fieldName) {
Function<T, U> func = null;
if ("name".equalsIgnoreCase(fieldName)) {
func = MyClass::getName;
} else if ("risk".equalsIgnoreCase(fieldName)) {
func = MyClass::getRisk;
}
return func;
}
The return type of my function is of Comparable type not sure what wrong I am doing here. Any pointers?