问题
How can we implement a compare method that compares Employee
s by their employee Id?
public int compare(Employee emp1, Employee emp2) {
throw new UnsupportedOperationException("Not supported yet.");
if(emp1.getEmpid()<emp2.getEmpid())
{
return -1;
}
else if(emp1.getEmpid()>emp2.getEmpid())
return 1;
else
return 0;
}
回答1:
public int compare(Employee emp1, Employee emp2) {
if(emp1==null || emp1==null ){
throw new UnsupportedOperationException("Not an Employee instance");
}
if(emp1.getEmpid()<emp2.getEmpid()){
return -1;
}
else if(emp1.getEmpid()>emp2.getEmpid()){
return 1;
}
return 0;
}
回答2:
remove throw new UnsupportedOperationException("Not supported yet.");
line from the code. It is added by your IDE(I think) and it is unnecessary here.
来源:https://stackoverflow.com/questions/18760391/how-can-we-implement-compare-method-of-comparator-class