简体   繁体   English

添加可比接口并添加 compareTo() 方法

[英]adding comparable interface and adding a compareTo() method

I'm having a bit of trouble adding the two noted above.我在添加上面提到的两个时遇到了一些麻烦。 I have two classes.我有两个班级。 Employee and Company.员工和公司。

The employee class holds some information about the employees, instance variables including their name, dates, numOfSales etc etc. And also some methods such as setName, getName and so on. employee class 包含一些关于员工的信息,实例变量包括他们的姓名、日期、numOfSales 等。还有一些方法,如 setName、getName 等。

The Company class creates an empty List.公司 class 创建一个空列表。 It then has a method which reads the list of employees from a text file - which is then how the list gets filled up.然后它有一个从文本文件中读取员工列表的方法——这就是列表的填充方式。

What I need to do is: sort the list by their numOfSales.我需要做的是:按他们的 numOfSales 对列表进行排序。 I first of all need to modify the employee class so that it implements the Comparable interface.我首先需要修改员工 class 使其实现 Comparable 接口。

public class Employee implements Comparable<Company>

I'm not sure if company or employee should go in there ^ <>?我不确定公司或员工是否应该在其中输入 go ^ <>? But when I try either, I get the error message that ' Employee is not abstract and does not override abstract method CompareTo in java.lang.Comparable ' What am I doing wrong here?但是当我尝试其中任何一个时,我收到错误消息“ Employee is not abstract and does not override abstract method CompareTo in java.lang.Comparable ”我在这里做错了什么? I must not changed any of the classes, they must not be changed to abstract or anything and I must not add any new ones.我不能更改任何类,不能将它们更改为抽象类或任何东西,我不能添加任何新类。

I then need to write a compareTo() method, I take it in the employee class?然后我需要写一个 compareTo() 方法,我把它放在员工 class 中? that will allow the employees to be sorted in ascending order of the value stored by their numOfSales instance variable.这将允许员工按其 numOfSales 实例变量存储的值的升序排序。

An Employee should be comparable to other Employee s, so it's Comparable<Employee> . Employee应该与其他Employee具有可比性,因此它是Comparable<Employee> You then need to implement the compareTo method, as the error message says:然后您需要实现compareTo方法,如错误消息所述:

public class Employee implements Comparable<Employee> {
    private int numOfSales;
    // other data members

    // Constructors, getters, setters, etc

    @Override
    public int compareTo(Employee e) {
        return Integer.compare(numOfSales, e.numOfSales);
    }
}

If you have somekind of List<Employee> the you can use the static method Collections.sort() to sort it without implementing the Comparable interface on the Employee class. You can simply use the variant of the sort function that accepts an Comparator object which will be used to compare two instances of the Employee class.如果你有某种List<Employee>你可以使用 static 方法Collections.sort()对其进行排序,而无需在Employee class 上实现Comparable接口。你可以简单地使用接受Comparator器 object 的排序 function 的变体将用于比较Employee class 的两个实例。

And you don't even need to implement one on your own.而且您甚至不需要自己实施一个。 You can simply use Comparator.comparingInt(e -> e.numOfSales) to create the correct Comparator to sort your list.您可以简单地使用Comparator.comparingInt(e -> e.numOfSales)创建正确的Comparator来对您的列表进行排序。

So所以

Collections.sort(employees, Comparator.comparingInt​(e -> e.numOfSales));

would be sufficent to sort your employee list.足以对您的员工列表进行排序。

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

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