简体   繁体   English

Java Collection.sort错误

[英]Error on Java Collection.sort

I am trying out Java Collections.sort method and get an error (please see in the code) which I don't understand. 我正在尝试Java Collections.sort方法,并得到一个我不理解的错误(请参见代码)。 Java says, "The method sort(List, Comparator) in the type Collections is not applicable for the arguments (List, StudentComparator)". Java说:“ Collections类型中的方法sort(List,Comparator)不适用于参数(List,StudentComparator)”。 I don't know what I am doing wrong here. 我不知道我在做什么错。

I have a class called Students and it looks like this. 我有一门叫做学生的课,它看起来像这样。

//import all necessary

public class Students implements Comparable<Students>{
public List<String> list;
public String firstname;
public String lastname;


public Students(){
    list = new ArrayList<String>();
}
public void addStudent(String firstname, String lastname){
    this.firstname = firstname;
    this.lastname = lastname;
    list.add(firstname + " " + lastname);
    Collections.sort(list); //This is fine though.
    Collections.sort(list, new StudentComparator()); //This gives me the error. I want to use this for custom comparison and in this case, by student's last name.

}

public String getFirstname() {
    return firstname;
}

public String getLastname() {
    return lastname;
}

@Override
public int compareTo(Students student) {
    return this.compareTo(student);
}


}

And I have another class that implements Comparator. 我还有另一个实现Comparator的类。

public class StudentComparator implements Comparator<Students>{

@Override
public int compare(Students student1, Students student2) {
    return student1.getLastname().compareTo(student2.getLastname());
}


}

I have no problem with using Anonymous Comparator interface and implement it right in the Students class, but when I create a new class that implements Comparator interface and try to sort the list, it gives me the error. 使用Anonymous Comparator接口并直接在Student类中实现它没有问题,但是当我创建一个实现Comparator接口的新类并尝试对列表进行排序时,它给了我错误。

You're trying to sort a List<String> with a Comparator<Students> , which evidently cannot work. 您正在尝试使用Comparator<Students>List<String>进行排序,这显然无法正常工作。 Perhaps you meant to use a List<Students> instead? 也许您打算使用List<Students>代替? Or a Comparator<String> ? 还是Comparator<String>

Also, you have this: 另外,您有:

@Override
public int compareTo(Students student) {
    return this.compareTo(student);
}

which will simply call itself indefinitely. 这将只是无限期地调用自己。 I'm not fully sure what you wanted to do there; 我不确定您要在那做什么? perhaps sort by the last name as you do in StudentComparator ? 也许像在StudentComparator一样按姓氏排序?

@Override
public int compareTo(Students student) {
    return getLastname().compareTo(student.getLastname());
}

I strongly recommend that you modify your Students bean and add the Comparable interface directly to it. 我强烈建议您修改学生bean并将Comparable接口直接添加到它。 Semantically speaking, Comparator works best when you need multiple sorting options. 从语义上讲,当您需要多个排序选项时,比较器效果最佳。 If you don't need multiple options for the same data type or if you want to provide a default sort then you should implement the Comparable interface. 如果同一数据类型不需要多个选项,或者要提供默认排序,则应实现Comparable接口。 It's less code and you don't have to do the prep work to get it going. 它的代码更少,您无需进行准备工作就可以进行操作。

mkyong has an excellent example of how Comparator and Comparable work: http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/ mkyong有一个很好的比较器和可比较器工作示例: http ://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

So, I cleaned up the code. 因此,我清理了代码。 Noticed how I set each Students's attribute inside the constructor? 注意我如何在构造函数中设置每个学生的属性? This is very good practice for setting attributes of the objects. 这是设置对象属性的很好的做法。 We needed import java.lang.Comparable; 我们需要import java.lang.Comparable; for 对于

public int compareTo(Students student){ 
    return this.getLastName().compareTo(student.lastName);
}

and we needed import java.util.Comparator; 我们需要import java.util.Comparator; for 对于

@Override
public int compare(Students student1, Students student2){
    return student1.getLastName().compareTo(student2.getLastName());
}
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.lang.Comparable;


public class Students implements Comparable<Students>{

public List<Students> list = new ArrayList<Students>();
public String firstName;
public String lastName;

public Students(String firstName, String lastName){
    this.firstName = firstName;
    this.lastName = lastName;
}

public void addStudent(String firstName, String lastName){
    list.add(new Students(firstName,lastName));
    Collections.sort(list); // this is fine though
    Collections.sort(list, new StudentComparator()); // this gives me an error
}

public String getFirstName(){
    return firstName;
}

public String getLastName(){
    return lastName;
}

@Override 
/* Compare a given student lastName to the current Object
 *  if greater than the current object, return -1
 *  if equals to the current object, return 0
 *  if less than the current object, return 1
 *  Here is Comparable below:
 */
public int compareTo(Students student){ 
    return this.getLastName().compareTo(student.lastName);
}
}

and we need import java.util.Comparator; 并且我们需要import java.util.Comparator; for 对于

public class StudentComparator implements Comparator<Students>{

@Override
    public int compare(Students student1, Students student2){
        return student1.getLastName().compareTo(student2.getLastName());
    }
}

Hope that explains it. 希望能解释一下。

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

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