关于覆写equals方法

由于长期使用lombok用注解会伴随着很多坑,也会让我们丢失javaBean规范,

我们公司用的mybtis模板生成po实体类和xml,今天在使用List<po>.contains时候一直flase,但是因为两边数据源来源 不一样,打开debug排查半天,明明每个字段值都一样但是还是false,然后这就是半个多小时过去了

/**
     * {@inheritDoc}
     *
     * @implSpec
     * This implementation iterates over the elements in the collection,
     * checking each element in turn for equality with the specified element.
     *
     * @throws ClassCastException   {@inheritDoc}
     * @throws NullPointerException {@inheritDoc}
     */
    public boolean contains(Object o) {
        Iterator<E> it = iterator();
        if (o==null) {
            while (it.hasNext())
                if (it.next()==null)
                    return true;
        } else {
            while (it.hasNext())
                if (o.equals(it.next()))
                    return true;
        }
        return false;
    }

当我看到 o.equals时候我瞬间明白了,我po里面肯定没有覆写equals,

接下来就是总结equals和"=="区别

然后就是下面两种覆写equls区别

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LineSubLine that = (LineSubLine) o;
        return Objects.equals(id, that.id) && Objects.equals(lineId, that.lineId) && Objects.equals(isFirst, that.isFirst) && Objects.equals(stationSId, that.stationSId) && Objects.equals(stationEId, that.stationEId) && Objects.equals(createTime, that.createTime) && Objects.equals(orderNum, that.orderNum);
    }

@Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值