Comparison method violates its general contract

排序问题排查Comparison method violates its general contract! 通过List.sort对元素进行排序,测试阶段没发现,在st测试的时候报了这个错误。“Comparison method violates its general contract!”。 具体堆栈信息: 看起来没什么问题,但是却报了一个错,“比较方法违反其一般合同”。 在 JDK7 版本以上,Comparator 要满足自反性,传递性,对称性,不然 Arrays.sort,Collections.sort会报 IllegalArgumentException 异常。 自反性:当 两个相同的元素. 阅读详情

Collections.sort(list, new Comparator<Integer>() {
	@Override
	public int compare(Integer o1, Integer o2) {
		// return o1 > o2 ? 1 : -1;
		return o1.compareTo(o2);// 正确的方式
	}
});

经过搜索得知,在JDK7以后,实现Comparable接口后,要满足一下三个特性:

1) 自反性:x,y 的比较结果和 y,x 的比较结果相反。

2) 传递性:x>y,y>z,则 x>z。

3) 对称性:x=y,则 x,z 比较结果和 y,z 比较结果相同。

collections.sort()方法异常,原因:

1. 比较对象存在null等。

if (this.xxx == null && o.xxx == null) {
    return 0;
}
if (this.xxx == null) {
    return 1;
}
if (o.xxx == null) {
    return -1;
}

2. 可能为JDK版本不兼容的问题,JDK7及以后可能会异常,因版本更新,未兼容老版本。

解决方法,排序前插入如下:

System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");

参考:

https://www.cnblogs.com/wendelhuang/p/7356797.html

使用Visual Studio 2022创建CUDA编程项目 本文介绍了在Visual Studio 2022中配置CUDA开发环境的完整流程。首先需要安装VS2022并勾选C++开发组件,然后安装CUDA Toolkit和兼容的NVIDIA驱动。创建项目时选择CUDA模板,VS会自动生成数组相加的示例代码。项目配置包含CUDA头文件依赖和链接器设置(cudart_static.lib)。文章详细说明了从环境搭建到代码编写、调试的完整步骤,帮助开发者快速上手CUDA编程。 阅读详情

相关推荐

Zabbix安装与部署 系统环境:centos7.0

Zabbix安装与部署 系统环境:centos7.0

weixin_42485027的博客 2600

帆软报错Comparison method violates its general contract!

红框的一对二,且下图红框D5的左父子格是B5,A5还设置排序是D5,导致报错。把D5的左父子格设置成A5即可解决。首先,我这边报错的原因是因为。

weixin_48277834的博客 423

【NLP】使用 PyTorch 从头构建自己的大型语言模型 (LLM)

如果我们在 n 维空间中绘制一个向量,看起来相似的物体(如狗和猫)将彼此靠近,而看起来不相似的物体(如学校和家庭)的嵌入向量将位于更远的地方。为此,我们将使用一种流行的标记器,称为 BPE 标记器,这是一种子词标记器,正在 GPT3 等模型中使用。:前馈网络使用深度神经网络来学习两个线性层(第一层有 d_model 节点,第二层有 d_ff 节点,根据注意力论文分配值)中嵌入向量的所有特征,并且将 ReLU 激活函数应用于第一线性层的输出,为嵌入值提供非线性,并应用 dropout 以进一步避免过度拟合。

sikh_0529的博客 1951

“Comparison method violates its general contract!”问题原因及解决办法

开发中踩到的坑。通过arraylist.sort对元素进行排序,测试阶段没发现,上线后不久报了这个错误。“Comparison method violates its general contract!”。查了一些资料,下面总结一下。先贴下出错的代码: stus.sort((o1, o2) -> { if(o1 == null || o2 == null || o1 == o2) { return 0; }

samur2的博客 5万+

固件测试工具选型需要考察的功能点汇总

通过专业的固件测试工具可以帮助我们提高测试效率、提升测试的覆盖度、确保测试的准确性,保障产品的安全和质量。本文我们讲固件测试工具的功能点进行梳理和汇总,方便大家在采购固件测试工具时进行选型比对。

daopuyun的博客 2112

Vue3+codemirror6实现公式(规则)编辑器

vue3+codemirror6+ts实现公式编辑器,可以插入标签、公式,也有补全提示

小阮的笔记本 5050

java异常:Comparison method violates its general contract解决

就在五一放假的前一天凌晨,收到一个测试同学发来的消息,说有个接口访问出现了问题,页面如下: 第二天早上紧急查看了线上日志,发现确实是因为这个告警导致返回问题 通过这条堆栈信息查了一些资料 Comparison method violates its general contract,是因为sort排序中重写compare方法引发的异常,在sort排序中重写的方法一定要满足:可逆比较 Comparator<Integer> c = (o1, o2) -> { if (o1.

许小乖……是总攻 9272

避坑指南:Unity与Visual Studio开发环境配置中的5个常见问题及解决方案

本文针对Unity与Visual Studio开发环境配置中的5个常见问题,提供了详细的解决方案。重点解决了项目文件生成失败导致命名空间无法识别、调试器无法附加、包管理器与扩展安装混乱、脚本编译错误以及多版本环境冲突等核心痛点,帮助开发者构建稳定高效的开发调试环境。

weixin_27791839的博客 281

java.lang.IllegalArgumentException: Comparison method violates its general contract!异常分析及解决办法

直接调用Collections的sort: Collections.sort(keywordList); 出现了如下错误: Exception in thread “main” java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.ComparableTimSort.mergeLo(ComparableTimSort.java:744) at java.util.

qq_39918677的博客 1万+

硬件工程师学习-硬件的单元测试(UT)(1)电源与时钟的测试

我们在设计的时候,通过控制电源的上电使能管脚或者通过MOSFET、或者专用的电源上电控制芯片,控制不同电源的上电时序,以确保每个芯片的电源上电时序是满足芯片的要求的。我们在测试的时候,需要对相关的要求一一测试,以确认每个上电时序满足要求的。在维基百科中,相位噪声的定义是“时域不稳定性(抖动)导致波形在相位上发生快速、短期、随机的波动,这种波动在频域中的表现即为相位噪声”。单元测试,一般是指基于开发人员自行开展的功能测试及各个功能单元的单元测试,是硬件信号级的测试,分为基本测试和信号完整性测试。

qq_43416206的博客 4034

Dify知识库文档批量下载神器:一键导出所有文件到本地

在使用Dify进行知识库管理的过程中,我们经常遇到一个实际问题:如何将已上传到知识库中的文档批量下载到本地?目前Dify的管理界面虽然功能强大,但暂时没有提供直接的UI界面来批量导出或下载知识库内的文档。这意味着如果我们需要将知识库中的文件备份到本地或进行离线处理,只能通过API接口来实现。本文将详细介绍如何利用Dify的REST API构建一个完整的知识库文档下载解决方案,这也是目前实现知识库文件批量下载到本地的唯一可行路径。🎉进入大模型应用与实战专栏 | 🚀查看更多专栏内容首先,我们需要在Dify的

羊城迷鹿的博客 3473

这个坑,你要注意:Comparison method violates its general contract!

背景有部分业务需要进行排序,对比的对象是某实体里的金额(double 类型),这样,我们实现了自定义的比较类,结果运行一段时间之后报了错误:Comparison method violates its general contract! ,经过校验,发现错误出现在自定义排序上,经网上各种查阅资料发现是jdk7的兼容问题,以下将解决过程分享给大家。错误截图:重写的比较方法:解决方案先说如何解决,解决

ESOO 3万+

3D LUT Color Conversion之总体架构设计之三

摘要:本文提出了一种基于三维坐标一维化寻址的四面体网格编码方法。通过定义全局网格坐标P(R,G,B)=R+9G+81B和局部增量(ξ,η,ζ)∈{0,1},推导出偏移量Δ(ξ,η,ζ)=ξ+9η+81ζ的平移不变性质。该方法将6种坐标排序情况(Case)对应的固定偏移量三元组预先存储,其中V0和V3的偏移量恒定为0和91。硬件实现时只需根据Case选择对应常量组,无需实时计算坐标,显著提高了处理效率。该方案利用线性寻址公式的特性,确保相同局部结构在不同位置具有一致的偏移模式。(149字)

大熊 49

Comparison method violates its general contract!

最近项目中有许多地方用到了集合排序,不是自然排序,必须的自己实现排序逻辑了。 java提供实现排序接口有两个Comparable与Comparator。 我简单理解: 1.Comparable接口是实现在需要排序的单个对象上的,例如:Student学生类,根据学号或其他属性排序,Student类就要implements实现Comparable接口,并重写int...

天生卷发 3万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

在 JDK7 版本以上,Comparator 要满足自反性,传递性,对称性

weixin_41653901的博客 1万+

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

java异常:Comparison method violates its general contract!解决

项目在线上运行时突然出现了几百封异常邮件,过了一段时间又自动好了。 项目异常报错信息是 Comparison method violates its general contract 在顺着堆栈信息查找下去,发现是sort排序中重写compare方法引发的异常。 在网上查找资料和阅读了sort的源码之后得出结论: 在sort排序中重写的方法一定要满足可逆比较 Comparator&amp;lt;Inte...

TomCosin的博客 5万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Comparison method violates its general contract!报错解决方法

今天遇到了一个Comparison method violates its general contract!问题,本来是同一个方法一个取了前十后十,一个是显示全部,然后一个报错,一个却能正常问题,排序代码如下 public Comparator down = new Comparator() { @Override public int compare(ZcglZlcgsyl o1, ZcglZ...

qq_36802747的博客 4271

解决 Comparison method violates its general contract!

问题:Comparison method violates its general contract!报错 1 Collections.sort(list, new Comparator<Integer>() { 2 @Override 3 public int compare(Integer o1, Integer o2) { 4 ...

dielucuan8830的博客 397

Java 排序异常 Comparison method violates its general contract

写了一段排序的代码,意思是o1对象的开始时间小于o2的开始时间时不交换o1,o2的顺序,否则交换顺序 if (o1.getTimeStart().getTime() < o2.getTimeStart().getTime()) { return -1; } else { return 1; ...

banshahuang5695的博客 916

java使用compareTo报Comparison method violates its general contract 异常

java.lang.IllegalArgumentException: Comparison method violates its general contract! 莫名其妙的出现一个错误,源代码如下: //对右侧栏进行排序 将 # 丢在最后 Collections.sort(mIndexDatas, new Comparator<String&g...

Zeno2017的博客 725

Comparison method violates its general contract! 神奇的报错

Comparison method violates its general contract

Stronger的博客 5055

idea项目报错Comparison method violates its general contract

idea项目报错Comparison method violates its general contract! 代码之前是没有任何问题的,早上一上班竟然就报错,然后我就百度了报错信息,看到了一个博主写的非常的详细,我就记录一下我的错误,转载,感谢! https://www.cnblogs.com/firstdream/p/7204067.html 报错信息 java.lang.IllegalArgumentException: Comparison method violates its general

qq_46056826的博客 495

图解JDK7的Comparison method violates its general contract异常

1.摘要 前一阵遇到了一个使用Collections.sort()时报异常的问题,跟小伙伴@zhuidawugui 一起排查了一下,发现问题的原因是JDK7的排序实现改为了TimSort,之后我们又进一步研究了一下这个神奇的算法。 2.背景 先说一下为什么要研究这个异常,前几天线上服务器发现日志里有偶发的异常: java.lang.IllegalArgumentEx

赶路人儿 6130
上一篇: java中分页思路
下一篇: eclispe创建新项目并部署到svn
hellorichen
博客等级 码龄13年 199粉丝 104原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值