Java 你如何比较课程的顺序?
我在寻找按顺序对类进行排序的有效方法时遇到了问题。下面的代码完成了排序的顺序,但我相信还有另一种方法(我不知道) 什么是对类进行排序的有效方法Java 你如何比较课程的顺序?,java,comparable,compareto,Java,Comparable,Compareto,我在寻找按顺序对类进行排序的有效方法时遇到了问题。下面的代码完成了排序的顺序,但我相信还有另一种方法(我不知道) 什么是对类进行排序的有效方法 public int compare(Object one, Object two) { //S = Salaried, W = Weekly, D = Daily //SS == 0 -> SW == -1 -> SD == -1 //WS == 1 -> WW
public int compare(Object one, Object two)
{
//S = Salaried, W = Weekly, D = Daily
//SS == 0 -> SW == -1 -> SD == -1
//WS == 1 -> WW == 0 -> WD == -1
//DS == 1 -> DW == 1 -> DD == 0
Employee a = (Employee)one;
Employee b = (Employee)two;
SalariedEmployee s = new SalariedEmployee(0.0);
WeeklyEmployee w = new WeeklyEmployee (0.0);
DailyEmployee d = new DailyEmployee();
if(one.getClass() == s.getClass() && two.getClass() == s.getClass())
return Double.compare(b.grossPay(), a.grossPay());
if(one.getClass() == s.getClass() && two.getClass() == w.getClass())
return -1;
if(one.getClass() == s.getClass() && two.getClass() == d.getClass())
return -1;
if(one.getClass() == w.getClass() && two.getClass() == s.getClass())
return 1;
if(one.getClass() == w.getClass() && two.getClass() == w.getClass())
return Double.compare(b.grossPay(), a.grossPay());
if(one.getClass() == w.getClass() && two.getClass() == d.getClass())
return -1;
if(one.getClass() == d.getClass() && two.getClass() == s.getClass())
return 1;
if(one.getClass() == d.getClass() && two.getClass() == w.getClass())
return 1;
if(one.getClass() == d.getClass() && two.getClass() == d.getClass())
return Double.compare(b.grossPay(), a.grossPay());
return 0;
}
在类中实现可比较的接口,并重写Employee类中的compareTo()
方法。该方法将对象类作为传递的值。比如说,
public class Employee implements Comparable<Employee> {
//omitted
public int compareTo(Employee other) {
return grossPay.compareTo(other.grossPay);
}
}
public类Employee实现了可比较的{
//省略
公共内部比较(员工其他){
返回grossPay.compareTo(其他grossPay);
}
}
查看以下链接了解更多信息
在类中实现可比较接口,并重写Employee类中的compareTo()
方法。该方法将对象类作为传递的值。比如说,
public class Employee implements Comparable<Employee> {
//omitted
public int compareTo(Employee other) {
return grossPay.compareTo(other.grossPay);
}
}
public类Employee实现了可比较的{
//省略
公共内部比较(员工其他){
返回grossPay.compareTo(其他grossPay);
}
}
查看以下链接了解更多信息
我在寻找按顺序对类进行排序的有效方法时遇到问题
这取决于你所说的“高效”是什么意思。从CPU的角度来看,将所有代码放在一个方法中是最有效的(如果做得好的话),但从灵活性的角度来看,效率不是很高
对于一种不会很快但会更灵活的方法,请查看和
GroupComparator允许您将多个比较器组合到一个排序中。BeanComparator是一个通用的比较器,允许您对给定类中的任何字段进行排序。因此,使用GroupComparator的基本代码是:
EmployeeComparator employee = new EmployeeComparator();
BeanComparator grossPay = new BeanComparator(Employee.class, "grossPay");
GroupComparator gc = new GroupComparator(employee, grossPay);
Collections.sort(list, gc);
因此,您需要编写一个比较器,按受薪员工、每周员工和每日员工进行排序。EmployeeComparator的基本代码可能类似于:
if (one.getClass()equals(two.getClass())
return 0;
if (one instanceOf SalariedEmployee)
return 1;
if (two instanceOf SalariedEmployee)
return -1;
if (one instanceOf WeeklyEmployee)
return 1;
else
return -1;
还有一些工作要设置,但是一旦有了EmployeeComparator,您就可以使用Bean和Group Comparator对多个不同的属性进行排序
我在寻找按顺序对类进行排序的有效方法时遇到问题
这取决于你所说的“高效”是什么意思。从CPU的角度来看,将所有代码放在一个方法中是最有效的(如果做得好的话),但从灵活性的角度来看,效率不是很高
对于一种不会很快但会更灵活的方法,请查看和
GroupComparator允许您将多个比较器组合到一个排序中。BeanComparator是一个通用的比较器,允许您对给定类中的任何字段进行排序。因此,使用GroupComparator的基本代码是:
EmployeeComparator employee = new EmployeeComparator();
BeanComparator grossPay = new BeanComparator(Employee.class, "grossPay");
GroupComparator gc = new GroupComparator(employee, grossPay);
Collections.sort(list, gc);
因此,您需要编写一个比较器,按受薪员工、每周员工和每日员工进行排序。EmployeeComparator的基本代码可能类似于:
if (one.getClass()equals(two.getClass())
return 0;
if (one instanceOf SalariedEmployee)
return 1;
if (two instanceOf SalariedEmployee)
return -1;
if (one instanceOf WeeklyEmployee)
return 1;
else
return -1;
还有一些工作要设置,但是一旦你有了EmployeeComparator,你就可以使用Bean和Group Comparator对多个不同的属性进行排序。这是我的解决方案
public int compare(Employee left, Employee right) {
int typeOrderLeft = getTypeOrder(left);
int typeOrderRight = getTypeOrder(right);
if (typeOrderLeft == typeOrderRight) {
return Double.compare(left.grossPay(), right.grossPay());
} else {
return typeOrderLeft - typeOrderRight;
}
}
private int getTypeOrder(Employee employee) {
if (employee instanceof DailyEmployee) {
return 1;
} else if (employee instanceof WeeklyEmployee) {
return 2;
} else if (employee instanceof SalaryEmployee) {
return 3;
}
return 0;
}
这是我的解决办法
public int compare(Employee left, Employee right) {
int typeOrderLeft = getTypeOrder(left);
int typeOrderRight = getTypeOrder(right);
if (typeOrderLeft == typeOrderRight) {
return Double.compare(left.grossPay(), right.grossPay());
} else {
return typeOrderLeft - typeOrderRight;
}
}
private int getTypeOrder(Employee employee) {
if (employee instanceof DailyEmployee) {
return 1;
} else if (employee instanceof WeeklyEmployee) {
return 2;
} else if (employee instanceof SalaryEmployee) {
return 3;
}
return 0;
}
您需要首先实现可比较的接口。这允许您定义一个compareTo方法,该方法可用于根据您认为可用于比较类的特定值对类进行排序
定义compareTo方法对于没有预定义的比较方法的对象非常有用。您需要首先实现comparable接口。这允许您定义一个compareTo方法,该方法可用于根据您认为可用于比较类的特定值对类进行排序
定义compareTo方法对于没有预定义的自我比较方式的对象很有用。排序顺序是SalaryEmployee->WeeklyEmployee->DailyEmployee作为第一个键grossPay作为第二个键排序顺序是SalaryEmployee->WeeklyEmployee->DailyEmployee作为第一个键grossPay作为第二个键我感谢您的支持然而,这只解决了我的粗俗问题,而不是课堂秩序。所以基本上有两个订单工资->每周->每日课程,然后是总工资。例如,每周工资500英镑,每天550英镑。我很感激你的回复,但是这只解决了我的问题,而不是班级秩序。所以基本上有两个订单工资->每周->每日课程,然后是总工资。例如,每周工资500英镑,每天550英镑。