java - Why my compare() method can not refer to a model class field or method? -
java - Why my compare() method can not refer to a model class field or method? -
i did defined employee class name , getsalary() method, when define mycomparator, recognize emp1 , emp2 employee class not recognize name , getsalary() method (dundefined).
import java.util.comparator; public class mycomparator<employee> implements comparator<employee> { @override public int compare(employee emp1, employee emp2) { string s = emp1.name; if (emp1.getsalary() < emp2.getsalary()) { homecoming -1; } else if (emp1.getsalary() == emp2.getsalary()) { homecoming 0; } else homecoming 1; } // compare method } // mycomparator class public class employee { private int salary; public string name; employee(string n, int s) { salary = s; name = n; } public int getsalary() { homecoming salary; } }
the right syntax :
public class mycomparator implements comparator<employee> { @override public int compare(employee emp1, employee emp2) { string s = emp1.name; if (emp1.getsalary() < emp2.getsalary()) { homecoming -1; } else if (emp1.getsalary() == emp2.getsalary()) { homecoming 0; } else homecoming 1; } // compare method }
you have replace mycomparator<employee>
mycomparator
you defining new generics, not using 1 created comparator.
java compare comparator
Comments
Post a Comment