我正在尝试对 List<> 对象进行排序,但抛出了此异常(尽管仅适用于大型列表)
排序代码:
List<FinalSentence> sentenceList = finalRepresentation.getSentences();
Collections.sort(sentenceList); // <=== EXCEPTION THROWN HERE!!!
FinalSentence 类头:
public class FinalSentence implements Comparable<FinalSentence>{...}
compareTo() 实现:
@Override
public int compareTo(FinalSentence o) {
if (this == o) {
return 0;
}
if (this.score > o.score) {
return 1;
}
if (this.score < o.score) {
return -1;
}
return 0;
}
这是一个例外:
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeHi(Unknown Source)
at java.util.ComparableTimSort.mergeAt(Unknown Source)
at java.util.ComparableTimSort.mergeCollapse(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at feature.finalRepresentation.Summarizer.summarize(Summarizer.java:30)
at driver.Driver.main(Driver.java:114)
对于一个小列表(少于 50 个元素)它可以工作。对于一个大列表(它也应该与那些一起工作)它会抛出这个异常。List 的实例类型是 ArrayList,这并不重要。
我不知道如何深究这一点。列表已满,元素的类型相同(那里没有多态性),但对于大型列表,我得到了这个奇怪的例外。
有任何想法吗?
先谢谢了!!!