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

时间:2015-01-10 17:56:44

标签: java sorting comparator

我正在开发一个小应用程序,我有一个"员工" class包含方法和相应的属性(name,salary,date ..)和另一个是雇员列表的类,我使用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);
            }};
            }

3 个答案:

答案 0 :(得分:1)

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

但是,您可以拥有多个实现该界面的类,并且只要您希望对Comparator<Employee>列表进行排序,就可以选择相关的Employee实现。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

感谢您的回复。好吧,我想我已经解决了,就是在类员工中使用这个代码然后在员工列表中实现这个功能,它已经有效了。

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


}




}