Java 比较法违反其一般合同例外

Java 比较法违反其一般合同例外,java,collections,comparator,Java,Collections,Comparator,我已检查此异常发生的原因。你能指引我吗?在下面的代码块中我做错了什么 Collections.sort(discountGroupDetailList, new Comparator<DiscountGroupDetail>() { long bdsIdOne; long bdsIdTwo; public int compare(DiscountGroupDetail discountGroup

我已检查此异常发生的原因。你能指引我吗?在下面的代码块中我做错了什么

        Collections.sort(discountGroupDetailList, new Comparator<DiscountGroupDetail>() {
            long bdsIdOne;
            long bdsIdTwo;

            public int compare(DiscountGroupDetail discountGroupDetailOne, DiscountGroupDetail discountGroupDetailTwo) {
                boolean discOne;
                boolean discTwo;
                SimpleDateFormat DATE_FORMATER = new SimpleDateFormat("yyyy/MM/dd");
                int compVal = 0;
                discOne = discountGroupDetailOne != null && discountGroupDetailOne.getServiceId() != null
                        && discountGroupDetailOne.getServiceId().getCodeAndVersion() != null
                        && discountGroupDetailOne.getServiceId().getCodeAndVersion().getCodeDetail() != null
                        && discountGroupDetailOne.getServiceId().getCodeAndVersion().getCodeDetail().getCode() != null;

                discTwo = discountGroupDetailTwo != null && discountGroupDetailTwo.getServiceId() != null
                        && discountGroupDetailTwo.getServiceId().getCodeAndVersion() != null
                        && discountGroupDetailTwo.getServiceId().getCodeAndVersion().getCodeDetail() != null
                        && discountGroupDetailTwo.getServiceId().getCodeAndVersion().getCodeDetail().getCode() != null;

                if (discOne == true && discTwo == true) {
                    bdsIdOne = MessageBuilderHelper.getBaseDiscountServiceById(discountGroupDetailOne.getServiceId())
                            .getBdsIdNbr();

                    bdsIdTwo = MessageBuilderHelper.getBaseDiscountServiceById(discountGroupDetailTwo.getServiceId())
                            .getBdsIdNbr();
                    compVal = (int) (bdsIdOne - bdsIdTwo);
                }



                DateRange dateRangeOne = discountGroupDetailOne != null ? discountGroupDetailOne.getDateSegment() : null;
                DateRange dateRangeTwo = discountGroupDetailTwo != null ? discountGroupDetailTwo.getDateSegment() : null;

                if (compVal == 0 && dateRangeOne != null && dateRangeTwo != null) {

                    Date date = new Date();
                    compVal = DATE_FORMATER.format(date.parse(dateRangeOne.getStartDate())).compareTo(
                            DATE_FORMATER.format(date.parse(dateRangeTwo.getStartDate())));
                }

                DiscountCode discountTypeOne = (discountGroupDetailOne != null && discountGroupDetailOne
                        .getVolumeDiscountGroupDetail() != null) ? DiscountCode.getDiscount(discountGroupDetailOne
                        .getVolumeDiscountGroupDetail().getType()) : null;
                DiscountCode discountTypeTwo = (discountGroupDetailTwo != null && discountGroupDetailTwo
                        .getVolumeDiscountGroupDetail() != null) ? DiscountCode.getDiscount(discountGroupDetailTwo
                        .getVolumeDiscountGroupDetail().getType()) : null;

                boolean isFXG = ("FXG".equals(discountGroupDetailOne.getServiceGeography()) || (discountGroupDetailOne
                        .getServiceId() != null && "FXG".equals(discountGroupDetailOne.getServiceId().getOperatingCompany())))
                        && ("FXG".equals(discountGroupDetailTwo.getServiceGeography()) || (discountGroupDetailTwo
                                .getServiceId() != null && "FXG".equals(discountGroupDetailTwo.getServiceId()
                                .getOperatingCompany())));

                if (compVal == 0 && discountTypeOne != null && discountTypeTwo != null && isFXG) {
                    compVal = ((int) (Integer.parseInt(discountTypeOne.getDiscountID()) - Integer.parseInt(discountTypeTwo
                            .getDiscountID())));
                }

                return compVal;
            }
        });
    }
Collections.sort(discountGroupDetailList,newcomparator(){
长双歧酮;
长两个;
公共整数比较(折扣组详细信息折扣组详细信息一、折扣组详细信息折扣组详细信息二){
布尔discOne;
布尔disc2;
SimpleDataFormat日期\格式化程序=新SimpleDataFormat(“yyyy/MM/dd”);
int compVal=0;
discOne=discountGroupDetailOne!=null&&discountGroupDetailOne.getServiceId()!=null
&&discountGroupDetailOne.getServiceId().getCodeAndVersion()!=null
&&折扣GroupDetailOne.getServiceId().getCodeAndVersion().getCodeDetail()!=null
&&折扣GroupDetailOne.getServiceId().getCodeAndVersion().getCodeDetail().getCode()!=null;
discTwo=discountGroupDetailTwo!=null&&discountGroupDetailTwo.getServiceId()!=null
&&discountGroupDetailTwo.getServiceId().getCodeAndVersion()!=null
&&折扣GroupDetailTwo.getServiceId().getCodeAndVersion().getCodeDetail()!=null
&&折扣GroupDetailTwo.getServiceId().getCodeAndVersion().getCodeDetail().getCode()!=null;
if(discOne==true&&discTwo==true){
bdsIdOne=MessageBuilderHelper.getBaseDiscountServiceById(discountGroupDetailOne.getServiceId())
.getBdsIdNbr();
BDSIDTOW=MessageBuilderHelper.getBaseDiscountServiceById(discountGroupDetailTwo.getServiceId())
.getBdsIdNbr();
compVal=(int)(bdsIdOne-bdsidtoo);
}
DateRange dateRangeOne=discountGroupDetailOne!=null?discountGroupDetailOne.getDateSegment():null;
DateRange DateRange2=discountGroupDetailTwo!=null?discountGroupDetailTwo.getDateSegment():null;
if(compVal==0&&dateRangeOne!=null&&dateRangeTwo!=null){
日期=新日期();
compVal=DATE\u formatter.format(DATE.parse(dateRangeOne.getStartDate())。与(
DATE_formatter.format(DATE.parse(dateRangeTwo.getStartDate());
}
DiscountCode discountTypeOne=(discountGroupDetailOne!=null&&discountGroupDetailOne
.GetVolumeDiscoveryGroupDetail()!=null)?折扣代码.getDiscount(折扣GroupDetailOne
.GetVolumeDiscoveryGroupDetail().getType()):null;
DiscountCode discountTypeTwo=(discountGroupDetailTwo!=null&&discountGroupDetailTwo
.GetVolumeDiscoveryGroupDetail()!=null)?折扣代码.getDiscount(折扣组详细信息二)
.GetVolumeDiscoveryGroupDetail().getType()):null;
布尔值isFXG=(“FXG”.equals(discountGroupDetailOne.getServiceGeography())| |(discountGroupDetailOne
.getServiceId()!=null&&“FXG”.equals(折扣组详细信息一.getServiceId().getOperatingCompany()))
&&(“FXG”.equals(折扣GroupDetailTwo.getServiceGeography())| |(折扣GroupDetailTwo
.getServiceId()!=null&&“FXG”.equals(折扣GroupDetailTwo.getServiceId()
.getOperatingCompany());
if(compVal==0&&discountTypeOne!=null&&discountTypeTwo!=null&&isFXG){
compVal=((int)(Integer.parseInt(discountTypeOne.getDiscountID())-Integer.parseInt(discountTypeTwo
.getDiscountID());
}
返回compVal;
}
});
}

这几乎总是由比较器不可传递或不不对称引起的。对于及物性,给定三个
DiscountGroupDetail
对象
a
b
c
,如果
a
先于
b
b
先于
c
,则总合同要求在所有情况下
a
先于
c
。同样,如果
a
等于
b
b
等于
c
,则要求
a
等于
c
。对于不对称性,交换不返回
0
的两个对象
a
b
的比较顺序必须返回相反符号的数字


考虑到你复杂的逻辑,我很难确定到底哪里出了问题,但以上是你需要检查的。

@immibis在我之前就发现了答案。概括这个原理:假设您正在编写一个比较多对键的比较器。您需要比较第一对,如果没有给出<或>答案,则比较第二对,如果仍然没有<或>,则比较第三对,依此类推

这里的规则是,在比较第一对密钥之后,除非第一对中的密钥相等,否则不能继续使用第二对密钥。在您的例子中,如果两者都有一个“代码”且代码相等,或者如果它们都为空,则它们是相等的。您的错误是,如果只有一个代码为空,您将继续使用下一对代码。在那种情况下,他们是不平等的。我猜你的想法是,如果一个项目没有代码,你就不能比较代码。但你必须这么做。您需要规定没有代码的项小于有代码的项(或大于;这取决于您希望在排序数组中看到它们的方式);你必须检查一下,如果左边的项目有空代码,返回一个负数,如果右边的项目有空代码,返回一个正数