前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 两个数组比较内容是否相等

Java 两个数组比较内容是否相等

作者头像
赵哥窟
发布2022-07-08 08:07:44
2K0
发布2022-07-08 08:07:44
举报
文章被收录于专栏:日常技术分享日常技术分享
需求

有两个数组,前提是数组对象是一样的。需要比较这两个数组中对象的值是否一致?需要考虑对象的顺序。如果对象里面在嵌套一个数组怎么处理。

实现

重写对象equals()和hashcode()方法

1.为什么要重写equals()方法? 因为object中的equals()方法比较的是对象的引用地址是否相等,当需要判断对象里的内容是否相等,则需要重写equals()方法。

2.重写equals()方法为什么要同时重写hashcode()方法? 重写equals()方法同时重写hashcode()方法,就是为了保证当两个对象通过equals()方法比较相等时,他们的hashCode值也一定要保证相等。

新增两个对象

@Data
@Slf4j
public class ProductBillingConfigCompare {

    /**
     * 费用类型
     */
    private String costType;

    /**
     * 结算周期
     */
    private String settlementInterval;

    /**
     * 计算方式
     */
    private String calculateWay;

    /**
     * 费用比例
     */
    private BigDecimal costValue;

    /**
     * 服务费列表
     */
    private List<ProductBillingConfigExt> billingConfigExtList;

    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;//地址相等
        }
        if(obj == null){
            return false;//非空性:对于任意非空引用x,x.equals(null)应该返回false。
        }

        if(obj instanceof ProductBillingConfigCompare){
            ProductBillingConfigCompare configCompare = (ProductBillingConfigCompare) obj;
            //比较两个对象嵌套数组的对象值,不考虑排序问题
            boolean listCompare = false;
            List<ProductBillingConfigExt> compareList = new ArrayList<>();
            //如果两个对象的数组都不为空
            if(!CollectionUtils.isEmpty(this.billingConfigExtList) && !CollectionUtils.isEmpty(configCompare.billingConfigExtList)){
                //当两个数组Size一样才比较对象的值是否相等,如果Size不一样直接false
                if(this.billingConfigExtList.size() == configCompare.getBillingConfigExtList().size()){
                    for (int i = 0; i < this.billingConfigExtList.size(); i++) {
                        ProductBillingConfigExt configExt1 =  this.billingConfigExtList.get(i);
                        for (int j = 0; j < configCompare.billingConfigExtList.size() ; j++) {
                            ProductBillingConfigExt configExt2 =  this.billingConfigExtList.get(j);
                            if(configExt1.equals(configExt2)){
                                compareList.add(configExt1);
                                break;
                            }
                        }
                    }
                }
                //如果比较结果数组和当前对象的数组Size一样则两个数组值一直
                if(this.billingConfigExtList.size() == compareList.size()){
                    listCompare = true;
                }
            }else if(CollectionUtils.isEmpty(this.billingConfigExtList) && CollectionUtils.isEmpty(configCompare.billingConfigExtList)){
                //两个对象数组都为空则对象嵌套的数组相同
                listCompare = true;
            }

            //需要比较的字段相等,则这两个对象相等
            if(this.costType.equals(configCompare.getCostType())
                    && this.settlementInterval.equals(configCompare.getSettlementInterval())
                    && this.calculateWay.equals(configCompare.getCalculateWay())
                    && this.costValue.equals(configCompare.getCostValue())
                    && listCompare){
                return true;
            }

        }

        return false;
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + (costType == null ? 0 : costType.hashCode());
        result = 31 * result + (settlementInterval == null ? 0 : settlementInterval.hashCode());
        result = 31 * result + (calculateWay == null ? 0 : calculateWay.hashCode());
        result = 31 * result + (costValue == null ? 0 : costValue.hashCode());
        result = 31 * result + (billingConfigExtList == null ? 0 : billingConfigExtList.hashCode());
        return result;
    }

}
@Data
public class ProductBillingConfigExt {

    /**
     * 服务费范围开始
     */
    private BigDecimal serviceFeeStart;

    /**
     * 服务费范围结束
     */
    private BigDecimal serviceFeeEnd;

    /**
     * 比例/固定值
     */
    private BigDecimal serviceFee;

    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;//地址相等
        }
        if(obj == null){
            return false;//非空性:对于任意非空引用x,x.equals(null)应该返回false。
        }

        if(obj instanceof ProductBillingConfigCompare){
            ProductBillingConfigExt configExt = (ProductBillingConfigExt) obj;
            //需要比较的字段相等,则这两个对象相等
            if(this.serviceFeeStart.equals(configExt.getServiceFeeStart())
                    && this.serviceFeeEnd.equals(configExt.getServiceFeeEnd())
                    && this.serviceFee.equals(configExt.getServiceFee())){
                return true;
            }
        }

        return false;
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + (serviceFeeStart == null ? 0 : serviceFeeStart.hashCode());
        result = 31 * result + (serviceFeeEnd == null ? 0 : serviceFeeEnd.hashCode());
        result = 31 * result + (serviceFee == null ? 0 : serviceFee.hashCode());
        return result;
    }
}

在写一个比较方法

public static boolean isListEqual(List<ProductBillingConfigCompare> list1, List<ProductBillingConfigCompare> list2) {
        // 两个list引用相同(包括两者都为空指针的情况)
        if (list1 == list2) {
            return true;
        }
        // 两个list都为空(包括空指针、元素个数为0)
        if ((list1 == null && list2 != null && list2.size() == 0)
                || (list2 == null && list1 != null && list1.size() == 0)) {
            return true;
        }
        // 两个list元素个数不相同
        if (list1.size() != list2.size()) {
            return false;
        }
        // 两个list元素个数已经相同,再比较两者内容
        // 采用这种可以忽略list中的元素的顺序
        // 涉及到对象的比较是否相同时,确保实现了equals()方法
        if (!list1.containsAll(list2)) {
            return false;
        }
        return true;
    }

调用

  boolean result = isListEqual(list1,list2);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-07-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 需求
  • 实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档