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

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -