比较器报错:Comparison method violates its general contract

Brother Zeng遇到的错误:

java.lang.IllegalArgumentException: Comparison method violates its general contract!

网上查到一个解释:

Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort has been replaced. The new sort implementation may throw an IllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently ignored such a situation. If the previous behavior is desired, you can use the new system property, java.util.Arrays.useLegacyMergeSort, to restore previous mergesort behavior.

也就是说jdk 7的sort函数的实现变了,造成了这个问题,具体原因未知。

改一下系统设置,还是选择使用老版本的排序方法,在代码前面加上这么一句话:System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");


另外复习一下比较器,一个简单的例子:

类代码:

[java]  view plain copy
  1. public class Stu {  
  2.     public double age;  
  3.     public String name;  
  4.     public Stu(String name,double age)  
  5.     {  
  6.         this.name = name;  
  7.         this.age = age;  
  8.     }  
  9. }  

主题代码

[java]  view plain copy
  1. import java.util.Collections;  
  2. import java.util.Comparator;  
  3. import java.util.List;  
  4.   
  5.   
  6. public class CodeForBrotherZeng {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.             Stu stu1=new Stu("foyd",22.1);  
  13.             Stu stu2=new Stu("teddy",19.1);  
  14.             Stu stu3=new Stu("dean",26.1);  
  15.             Stu stu4=new Stu("lucas",19.1);  
  16.             Stu stu5=new Stu("tina",26.1);  
  17.               
  18.             List<Stu> list = new ArrayList<Stu>();  
  19.             list.add(stu1);  
  20.             list.add(stu2);  
  21.             list.add(stu3);  
  22.             list.add(stu4);  
  23.             list.add(stu5);  
  24.               
  25.             Comparator<Stu> comparator = new Comparator<Stu>() {  
  26.                 public int compare(Stu p1, Stu p2) {//return必须是int,而str.age是double,所以不能直接return (p1.age-p2.age)  
  27.                     if((p1.age-p2.age)<0)   
  28.                         return -1;  
  29.                     else if((p1.age-p2.age)>0)  
  30.                         return 1;  
  31.                     else return 0;  
  32.                 }  
  33.             };  
  34.             //jdk 7sort有可能报错,  
  35.             //加上这句话:System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");  
  36.             //表示,使用以前版本的sort来排序  
  37.             Collections.sort(list,comparator);  
  38.               
  39.             for(int i=0;i<list.size();i++)  
  40.             {  
  41.                 System.out.println(list.get(i).age+"  "+list.get(i).name);  
  42.             }  
  43.               
  44.     }  
  45.   
  46. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值