我们如何实现比较器类的比较方法 [英] How can we implement compare method of comparator class
本文介绍了我们如何实现比较器类的比较方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何实现一个比较方法,比较 Employee
与其员工ID?
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;
}
推荐答案
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;
}
这篇关于我们如何实现比较器类的比较方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文