简体   繁体   English

如何实现比较器

[英]How to implement comparator

I was given the code below as Homework. 下面给出了我作为家庭作业的代码。 I am being asked to implement comparator for employee objects. 我被要求为员工对象实现比较器。 The compare method returns an int. compare方法返回一个int。 However, none of the methods that I have in my employee class return an int if you compare them. 但是,如果比较它们,我的员工类中没有的方法都不会返回int。 Can anyone give me some guidance on how the compare method is supposed to work? 谁能给我一些比较方法应该如何工作的指导? Thank you 谢谢

import java.util.*;
import java.io.*;
public class EmployeeClient
{
//A Comparator for Employees
// Primary key   : Employee category - Salaried > Weekly > Daily
// Secondary key : Employee gross pay
private static class EmployeeComparator implements Comparator
{
  public int compare(Object one, Object two)
  {   
      Employee uno = (Employee) one;
      Employee dos = (Employee) two;

  }
}

public abstract class Employee {

private String idNumber;
private double payRate;

//Accessor: Return the id number of employee
public String getidNumber()
{
    return idNumber;
}

//Accessor: Return the payrate of the employee
public double getpayRate()
{
    return payRate;
}

public String toString()
{
    return getidNumber()+" "+getpayRate();
}

public abstract double grossPay();

}

I would suggest checking out this link . 我建议检查一下此链接

The goal of a custom comparator is to take two objects and determine how they relate to one another. 定制比较器的目标是获取两个对象并确定它们之间的关系。 Consider it an ordering problem. 将其视为订购问题。 Say I have two bags with red and blue marbles in them. 说我有两个装着红色和蓝色大理石的袋子。 If I want to do a comparison of the bags in terms of blue marbles, it is the same as asking which bag has more/less blue marbles in it. 如果我要比较蓝色大理石上的袋子,就等于询问哪个袋子中有更多/更少的蓝色大理石。

The return value of the compare function determines which object is valued higher than the other. 比较函数的返回值确定哪个对象的值高于另一个对象。 let ret be the value after comparing the two objects. ret为比较两个对象后的值。

  • ret > 0 means the left object is valued higher than the right ret > 0表示左对象的值高于右对象
  • ret < 0 means the right object is valued higher than the left ret < 0表示右对象的值高于左对象
  • ret == 0 means the two objects are valued the same ret == 0表示两个对象的值相同

Hope this helps. 希望这可以帮助。

Edit: 编辑:

In the case you are not allowed or unable to modify the Employee class, do the comparisons in your compare function in your first set of code. 如果不允许或无法修改Employee类,请在第一组代码中的compare函数中进行比较。 For example, if you wanted to compare two integers int a and int b , you would do: 例如,如果要比较两个整数int aint b ,则可以执行以下操作:

a.compareTo(b)

or 要么

Integer.compare(a, b)

  • If a > b : either method would return a value greater than 0 如果a > b :任一方法将返回大于0的值
  • If a < b : either method would return a value less than 0 如果a < b :任一方法将返回小于0的值
  • If a == b : either method would return 0 如果a == b :任一方法将返回0

Simply apply this idea to your code 只需将此想法应用于您的代码

Actually, all of the fields in your Employee class return ints when compared. 实际上,在比较时,Employee类中的所有字段都返回int。

Also, each Employee method returns an object that can be compared using compareTo() to return an int. 同样,每个Employee方法都返回一个对象,可以使用compareTo()比较该对象以返回int。

Your Employee class has two fields: 您的Employee类有两个字段:

String idNumber;
double payRate;

Both String and Double (the object form of double) have a compareTo() method that returns an int. StringDouble (double的对象形式)都具有compareTo()方法,该方法返回int。

So... Your EmployeeComparator.compare() method could simply return: 所以...您的EmployeeComparator.compare()方法可以简单地返回:

one.getidNumber().compareTo(two.getidNumber());

Here's how you could rewrite your Comparator class: 这是重写重写器类的方法:

private static class EmployeeComparator implements Comparator<Employee>
{
  public int compare(Employee empOne, Employee empTwo)
  {   
      return empOne.getidNumber().compareTo(empTwo.getidNumber());
  }
}

The comparator above bases comparison strictlly on employee id number. 上面的比较器严格根据员工编号进行比较。

But... It should be obvious how to modify it to consider other fields. 但是...很明显,如何修改它以考虑其他领域。

Well it always depends on what property you want to sort. 好吧,它始终取决于您要排序的属性。 For generic entities like these I would suggest its internal id. 对于此类通用实体,我建议使用其内部ID。 BUT: The Employee class is actually poorly designed. 但是:Employee类实际上设计得很差。 getIdNumber() should return an int not a String . getIdNumber()应该返回int而不是String If you can redesign the class, do it! 如果您可以重新设计课程,那就去做吧! If not parse the string using Integer.parseInt() , then youll have an int for comparsion. 如果不使用Integer.parseInt()解析字符串,则将有一个用于比较的int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM