Java - 等于列表返回 false?

我有这个代码比较两个列表是否具有相同的对象:


List<CcustoPrestadorOuImplDTO> implsNaConfig = configImplPermitida.getImplementos();

List<CcustoPrestadorOuImplDTO> implsNoApto = configuracaoImplementoDoApontamento.getImplementos();

Collections.sort(implsNaConfig, Comparator.comparing(o -> o.getCdCcusto()));

Collections.sort(implsNoApto, Comparator.comparing(o -> o.getCdCcusto()));


if ( implsNaConfig.equals(implsNoApto)  ){

    return true;

}

在调试时我有这种情况:

http://img4.mukewang.com/618caaaa000115d604030255.jpg

如您所见,两个列表都具有相同属性的相同对象。

但是比较两个列表是否相等的代码总是返回false。

我尝试使用 containsAll() 方法,但由于某种原因也返回 false。

我究竟做错了什么?


Cats萌萌
浏览 187回答 1
1回答

狐的传说

正如@DawoodibnKareem 所问,我将发布解决方案:总是在其中得到“false”的原因是if ( implsNaConfig.equals(implsNoApto)&nbsp; )因为我的CcustoPrestadorOuImplDTO类没有equals实现该方法。所以我编辑了类文件并自动生成了 equals 方法并且它起作用了。CCustoPrestadorOuImplDTO 类中的 equals 方法:@Overridepublic boolean equals(Object o) {&nbsp; &nbsp; if (this == o) return true;&nbsp; &nbsp; if (o == null || getClass() != o.getClass()) return false;&nbsp; &nbsp; CcustoPrestadorOuImplDTO that = (CcustoPrestadorOuImplDTO) o;&nbsp; &nbsp; return Objects.equals(cdCcusto, that.cdCcusto) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Objects.equals(deCcusto, that.deCcusto) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Objects.equals(grupoOperativo, that.grupoOperativo) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Objects.equals(deGrupoOperativo, that.deGrupoOperativo);}这是 HashCode() 方法:@Overridepublic int hashCode() {&nbsp; &nbsp; return Objects.hash(cdCcusto, deCcusto, grupoOperativo, deGrupoOperativo);}这真的很简单,但我什至不认为这是问题的原因。谢谢大家。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java