Java Collection.sort出错

时间:2014-05-27 13:53:30

标签: java sorting collections

我正在尝试Java Collections.sort方法并得到一个我不明白的错误(请参阅代码中)。 Java说,“类型集合中的方法排序(List,Comparator)不适用于参数(List,StudentComparator)”。我不知道我在这里做错了什么。

我有一个名为Students的课程,它看起来像这样。

//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);
}


}

我还有另一个实现Comparator的类。

public class StudentComparator implements Comparator<Students>{

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


}

使用Anonymous Comparator接口并在Students类中实现它没有问题,但是当我创建一个实现Comparator接口并尝试对列表进行排序的新类时,它会给我错误。

3 个答案:

答案 0 :(得分:4)

您尝试使用List<String>Comparator<Students>进行排序,显然无法正常工作。也许您打算使用List<Students>代替?或Comparator<String>

另外,你有这个:

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

只会无限期地自我调用。我不完全确定你想在那里做什么;或许按照StudentComparator中的姓氏排序?

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

答案 1 :(得分:0)

我强烈建议您修改Students bean并直接添加Comparable界面。从语义上讲,当您需要多个排序选项时,Comparator最有效。如果您不需要同一数据类型的多个选项,或者如果您想提供默认排序,那么您应该实现Comparable接口。它的代码较少,您无需进行准备工作即可实现。

mkyong有一个关于Comparator和Comparable如何工作的很好的例子:http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

答案 2 :(得分:0)

所以,我清理了代码。注意我如何在构造函数中设置每个学生的属性?这是设置对象属性的非常好的做法。

我们需要import java.lang.Comparable;
public int compareTo(Students student){ 
    return this.getLastName().compareTo(student.lastName);
}

我们需要import java.util.Comparator; 为了

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

我们需要import java.util.Comparator;

public class StudentComparator implements Comparator<Students>{

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

希望能够解释它。