作为重载,该方法比较比较器接口



>我正在开发一个小应用程序,我有一个带有方法和相应属性(姓名、薪水、日期..)的"Employee"类和另一个是员工列表的类,我用arraylist来存储员工和不同的方法,比如添加员工、删除员工。

现在我正在尝试按字母顺序和雇用日期(从低到高)对员工进行排序。

但是我有一个小问题,我不能使用该方法比较超过2次,我一直在研究,而不是它是否可以使该方法超载,以按姓名和日期对员工进行排序。谢谢。

    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 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);
            }};
            }

您不能重载 Comparator<Employee> 接口的 compare 方法,因为该方法只有一个签名 - int compare(Employee s1, Employee s2)

但是,您可以有多个实现该接口的类,并且您可以随时选择相关的Comparator<Employee>实现,以便对Employee列表进行排序。

您不需要

两种方法,您只需要具有逻辑的单一方法首先考虑名称,然后再考虑雇用日期:

@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());
} 

感谢您的回复。好吧,我想我已经解决了,也就是说,在班级员工中使用此代码,然后在员工列表中实现该功能,它已经奏效了。

 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();

}


}

最新更新