Java 作为重载方法比较比较器接口?
我正在开发一个小应用程序,我有一个“Employee”类,其中包含方法和相应的属性(姓名、薪水、日期…),还有一个类是员工列表,我使用arraylist存储员工和不同的方法,例如添加员工、删除员工 现在我正试图按字母顺序和雇佣日期(从低到高)对员工进行排序 但我有一个小问题,我不能使用该方法比较超过2次,我一直在研究,而不是它是否可以超载的方法比较排序的姓名和日期的员工。 谢谢 //------------------------------------------------------Java 作为重载方法比较比较器接口?,java,sorting,comparator,Java,Sorting,Comparator,我正在开发一个小应用程序,我有一个“Employee”类,其中包含方法和相应的属性(姓名、薪水、日期…),还有一个类是员工列表,我使用arraylist存储员工和不同的方法,例如添加员工、删除员工 现在我正试图按字母顺序和雇佣日期(从低到高)对员工进行排序 但我有一个小问题,我不能使用该方法比较超过2次,我一直在研究,而不是它是否可以超载的方法比较排序的姓名和日期的员工。 谢谢 //------------------------------------------------------
public class listEmployee implements Comparator <Employee>
{
ArrayList<Employee> Employees = new ArrayList<>();
@Override
public int compare(Employee e1, Employee e2) {
if (e1.getName() != e2.getName()) {
return 1;
} else {
return -1;
}
}
// now , when I call this function in the parent class , if you sort the names , now when I try
//to sort dates , not let me use the method compare more than twice
public void sortAlphabetical() {
Collections.sort(trabajadores,Employee.StuNameComparator);
}
//I was looking online and found these options, but not as a call in the main class
public static Comparator <Employee > sortDate = new Comparator<Employee >()
{
@Override
public int compare (Employee s1, Employee s2)
{
GregorianCalendar empTemporal = s1.getDateHire();
GregorianCalendar emTemporal2 = s2.getDateHire();
return empTemporal.compareTo(emTemporal2);
}
};
public static Comparator <Employee> StuNameComparator = new Comparator<Employee >() {
@Override
public int compare(Employee s1, Employee s2) {
String EmployeName1 = s1.getName().toUpperCase();
String EmployeName2 = s2.getName().toUpperCase();
//ascending order
return EmployeName1.compareTo( EmployeName2);
//descending order
//return StudentName2.compareTo(StudentName1);
}};
}
公共类listEmployee实现了Comparator
{
ArrayList Employees=新的ArrayList();
@凌驾
公共整数比较(员工e1、员工e2){
如果(e1.getName()!=e2.getName()){
返回1;
}否则{
返回-1;
}
}
//现在,当我在父类中调用此函数时,如果您对名称进行排序,现在当我尝试
//要对日期进行排序,请不要让我使用方法比较两次以上
公共无效sortAlphabetical(){
集合。排序(trabajadores、Employee、StuNameComparator);
}
//我在网上找到了这些选项,但不是在主课上
公共静态比较器sortDate=新比较器()
{
@凌驾
公共整数比较(员工s1、员工s2)
{
GregorianCalendar empTemporal=s1.getDateHire();
GregorianCalendar emTemporal2=s2.getDateHire();
返回empTemporal.compareTo(emTemporal2);
}
};
公共静态比较器=新比较器(){
@凌驾
公共整数比较(员工s1、员工s2){
字符串EmployeName1=s1.getName().toUpperCase();
字符串EmployeName2=s2.getName().toUpperCase();
//升序
返回EmployeName1.compareTo(EmployeName2);
//降序
//返回StudentName2.compareTo(StudentName1);
}};
}
您不能重载比较器
接口的比较
方法,因为该方法只有一个签名-整数比较(员工s1,员工s2)
但是,您可以有多个类来实现该接口,并且您可以选择相关的
比较器
实现,只要您希望对员工
列表进行排序。您不需要两种方法,您只需要使用一种逻辑方法,首先考虑姓名,然后考虑雇用日期:
@Override
public int compare(Empleado e1, Empleado e2) {
int nameCompare = e1.getName().compareTo(e2.getName());
if (nameCompare != 0) {
return nameCompare;
}
return e1.getDateHire().compareTo(e2.getDateHire());
}
谢谢你的回复。我想我已经解决了,也就是说,在employee类中使用这个代码,然后在employee列表中实现这个函数,它已经成功了
public class Employee
{
private String name;
private GregorianCalendar dateHire;
public GregorianCalendar getDateHire() { return dateHire; }
public void setDateHire(GregorianCalendar dateHire) { this.dateHire = dateHire; }
public getName() { return name;}
public setName (String name) { this.name = name;}
public static Comparator <Employee > HireDateComparator = new Comparator<Employee >()
{
@Override
public int compare (Employees1, Employee s2)
{
GregorianCalendar empTemporal = s1.getfechaContratacion();
GregorianCalendar emTemporal2 = s2.getfechaContratacion();
empTemporal.get(Calendar.YEAR);
emTemporal2.get(Calendar.YEAR);
return empTemporal.compareTo(emTemporal2);
}
};
public static Comparator <Employee > StuNameComparator = new Comparator<Employee >() {
@Override
public int compare(Employee s1, Employee s2) {
String StudentName1 = s1.getName().toUpperCase();
String StudentName2 = s2.getName().toUpperCase();
//ascending order
return StudentName1.compareTo(StudentName2);
//descending order
//return StudentName2.compareTo(StudentName1);
}};
}
你用西班牙语和英语把我弄糊涂了。雇员,你的班级叫什么名字?就业??请坚持这样或那样。请在发布问题之前进行校对。是的,解决方案是使用Comparable实现类的“自然排序”,并在需要以非类的“自然排序”的方式排序时使用Comparator(如果需要多个,则使用Comparator)。那么,您当前的代码是如何不起作用的呢?也许它不起作用,因为您比较了对象
=代码>不是字符串.equals()
。对不起,我已经更正了
public class Employee
{
private String name;
private GregorianCalendar dateHire;
public GregorianCalendar getDateHire() { return dateHire; }
public void setDateHire(GregorianCalendar dateHire) { this.dateHire = dateHire; }
public getName() { return name;}
public setName (String name) { this.name = name;}
public static Comparator <Employee > HireDateComparator = new Comparator<Employee >()
{
@Override
public int compare (Employees1, Employee s2)
{
GregorianCalendar empTemporal = s1.getfechaContratacion();
GregorianCalendar emTemporal2 = s2.getfechaContratacion();
empTemporal.get(Calendar.YEAR);
emTemporal2.get(Calendar.YEAR);
return empTemporal.compareTo(emTemporal2);
}
};
public static Comparator <Employee > StuNameComparator = new Comparator<Employee >() {
@Override
public int compare(Employee s1, Employee s2) {
String StudentName1 = s1.getName().toUpperCase();
String StudentName2 = s2.getName().toUpperCase();
//ascending order
return StudentName1.compareTo(StudentName2);
//descending order
//return StudentName2.compareTo(StudentName1);
}};
}
public class listEmployee
{
ArrayList<Employee> Employees = new ArrayList<>();
public void sortAlphabetical() {
Collections.sort( Employees, Employee.StuNameComparator);
}
public void sortByDate() {
Collections.sort(Employees, Employee.HireDateComparator);
}
}
public class ArrayList {
public static void main(String[] args)
{
listEmployee createEmployes = new listEmployee ();
createEmployes.sortAlphabetical();
createEmployes.sortByDate();
}
}