`
lxwt909
  • 浏览: 570755 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Comparison method violates its general contract

阅读更多

     今天一个群里哥们儿碰到一个异常,抛到群里求解答,他的代码如下图:

抛出的异常信息为:

java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:868)
at java.util.TimSort.mergeAt(TimSort.java:485)
at java.util.TimSort.mergeCollapse(TimSort.java:408)
at java.util.TimSort.sort(TimSort.java:214)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:659)
at java.util.Collections.sort(Collections.java:217)

 
 我说是compare方法实现的问题,他死活跟我掰,说我之前代码还好好的啊。没办法,我只好根据异常信息提示去翻JDK源码,异常里提示at java.util.TimSort.mergeHi(TimSort.java:868)即TimSort类的mergeHi方法抛出的。于是我不断Google,找到了这篇帖子why does my compare method throw exception — Comparison method violates its general contract》,根据他们的提示,我大概了解了compare方法需要返回1,-1,0即你的返回值要符合约定。

    于是我又按照异常提示看了Collections的sort方法源码,如图:

 继续跟踪Arrays类的sort方法:

 看到这里我基本就豁然开朗了,因为抛异常的地方是在TimSort类里,说明实际走的是else分支,所以有了第一种解决方法,添加-Djava.util.Arrays.useLegacyMergeSort=true这个JVM参数,其实要真正解决这个问题,要符合规范的实现compare方法,因为他写的代码里没有考虑对象o1和对象o2为Null的情况,即当o1与o2都为null时两者大小如何判定呢,当o1为null但o2不为null时两者大小又如何判定了呢,同理当o2为null但o1不为null时两者大小又如何判定呢又不得而知,或许你又会说,我这两个对象不可能为null,但那是你认为,JVM不知道,它只要求你的逻辑必须严谨,严格考虑各种情况下两者大小的判定原则。所以正确写法应该是:

if(o1 == null && o2 == null) {
	return 0;
}
if(o1 == null) {
	return -1;
}
if(o2 == null) {
	return 1;
}
if(o1.getCreateTime() > o2.getCreateTime()) {
	return 1;
}
if(o2.getCreateTime() > o1.getCreateTime()) {
	return -1;
}
return 0;

  

  • 大小: 10.1 KB
  • 大小: 25.3 KB
  • 大小: 24.5 KB
分享到:
评论

相关推荐

    java中排序报:Comparison method violates its general contract异常的解决

    在Java编程中,当执行排序操作时,可能会遇到一个特定的异常:“Comparison method violates its general contract”。这个异常通常发生在使用`Arrays.sort()`或`Collections.sort()`方法时,尤其是在从Java 6升级到...

    Algorithms for Association Rule Mining - A General Survey and Comparison

    ### 关联规则挖掘算法:概览与比较 #### 引言 自1993年引入以来,关联规则挖掘已成为数据挖掘领域中最受关注的任务之一。这一方法在知识发现(KDD)过程中,尤其是在市场篮子分析等商业场景下,被广泛应用。...

    Closely interleaved self-comparison method applied to precise measurement

    A self-comparison method with closely interleaved switching states is analyzed and used to evaluate some type-B uncertainties of an 87Rb atomic fountain clock. Free from additional frequency reference...

    The Boundary Element Method with Programming

    The often quoted comparison that the method is a “Cinderella”, dominated by her “big sister”, the Finite Element Method, and whose beauty is hidden away, is still true and we hope that the reader...

    Comparison of regional planning strategies: Countywide general plans in USA

    标题《Comparison of regional planning strategies: Countywide general plans in USA》和描述《The regional plans of developed countries follow a few common patterns. Both subjects under research have ...

    Hex Comparison1.8.2破解版

    Hex Comparison(文件比较与编辑)是一款小巧的十六进制文件比较和编辑修改工具。可以同时打开两个文件在同一界面比较,它可以自动比较两个文件的不同之处并高亮显示。并可以把不同之处生成文件保存成文档。

    A low-medium frequency sound velocity measurement device and measurement result via comparison method

    根据给定的文件信息,以下是对标题、描述和部分内容中提及的知识点的详细说明: 标题和描述中都提到了“低中频声速测量设备及比较法测量结果”,这表明研究的焦点是设计并测试一个用于在水下测量声速的装置,并通过...

    Hex Comparison

    Hex Comparison 是一款二进制文件比较与十六进制编辑工具。它可以帮助你使用二进制格式来比较两个文件。主要功能: 1.允许快速比较两个文件。 2.快速查找每一处不匹配的数据。 3.使用自定义颜色来显示不匹配数据。 4...

    Sorting Running Time Comparison Test

    本文将深入探讨四种常见的排序算法:插入排序、合并排序、堆排序和快速排序,并通过"Sorting Running Time Comparison Test"来分析它们的运行性能。 1. 插入排序(Insertion Sort): 插入排序是一种简单直观的排序...

    tiusie.zip_Ambiguity function_The Method Method

    This is the second energy entropy matlab code, EULER numerical analysis method, Comparison of soft threshold and hard threshold and today various threshold calculation method.

    Comparison of learning disabled and general education children on the McCarthy Scales of Children's Abilities

    Comparison of learning disabled and general education children on the McCarthy Scales of Children's Abilities P.vyrhology in the Srhools 1980, 17. 429-436 COMPARISON OF LEARNING DISABLED AND ...

    fandango_score_comparison.csv

    fandango_score_comparison.csv 。python 机器学习,电影评分表。

    Algorithm-sorting-algorithms-performance-comparison.zip

    Algorithm-sorting-algorithms-performance-comparison.zip,一组排序算法的性能比较它计算每个算法用于排序列表的时间:已排序的数字和未排序的数字,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则...

    comparison of db2 vs oracle

    ### DB2与Oracle数据库对比分析 #### 引言:为何选择IBM DB2? 随着全球数字化进程的加速,企业面临着前所未有的数据处理挑战。为了更好地利用信息技术(IT),许多组织正在寻求更智能、更高效的解决方案来应对日益...

    Digital Change Detection by Post-Classification Comparison of RS Data in Land Use of Guangzhou

    Digital Change Detection by Post-Classification Comparison of RS Data in Land Use of Guangzhou,樊风雷,Wang Yunpeng,Remote sensing has long time been an important component of regional planning for ...

    atlassian-visual-comparison-2.5.4-jira20.jar

    atlassian-visual-comparison-2.5.4-jira20.jar webdriver

    fandango_score_comparison

    标题"Fandango_score_comparison"可能指的是一个关于电影评分比较的研究或数据分析项目,其中可能涉及到Fandango网站上的电影评分与其他来源(如Metacritic或Rotten Tomatoes)的评分对比。描述提到“房价信息”,这...

    Arm Cortex-M Comparison Table_v3.pdf

    ### ARM Cortex-M处理器系列对比分析 #### 概述 ARM Cortex-M处理器家族是为成本与能源效率极高的微控制器优化设计的。这些处理器被广泛应用于各类设备中,包括物联网(IoT)、工业以及日常消费电子产品等。...

    C# comparison

    C# 是一种面向对象的编程语言,由微软公司开发,主要应用于Windows平台,尤其是在.NET框架下。本PPT总结了C#与Java、VB、C++的一些基础差异和相似性,以及新语言特性与高级话题。 一、基础差异与相似性 ...

Global site tag (gtag.js) - Google Analytics