由于长期使用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);
}