比较器报错: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”);


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

类代码:


    
    
  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. }

主题代码


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



  


转自: https://blog.csdn.net/fanzitao/article/details/8040201

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”);


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

类代码:


  
  
  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. }

主题代码


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



  

猜你喜欢

转载自blog.csdn.net/u013651026/article/details/82019319