首先需要重写equals方法和hash方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HjHsCode hjHsCode = (HjHsCode) o;
return Objects.equals(hsCode, hjHsCode.hsCode) &&
Objects.equals(cnName, hjHsCode.cnName) &&
Objects.equals(enName, hjHsCode.enName) &&
Objects.equals(unit, hjHsCode.unit) &&
Objects.equals(unit2, hjHsCode.unit2) &&
Objects.equals(controlMa, hjHsCode.controlMa) &&
Objects.equals(ciqMa, hjHsCode.ciqMa) &&
Objects.equals(ggModel, hjHsCode.ggModel) &&
Objects.equals(imLowRate, hjHsCode.imLowRate) &&
Objects.equals(imNormalRate, hjHsCode.imNormalRate) &&
Objects.equals(imTempRate, hjHsCode.imTempRate) &&
Objects.equals(imTaxRate, hjHsCode.imTaxRate) &&
Objects.equals(exReturnRate, hjHsCode.exReturnRate) &&
Objects.equals(exNormalRate, hjHsCode.exNormalRate) &&
Objects.equals(exTempRate, hjHsCode.exTempRate) &&
Objects.equals(exSpecialRate, hjHsCode.exSpecialRate);
}
@Override
public int hashCode() {
return Objects.hash(hsCode, cnName, enName, unit, unit2, controlMa, ciqMa, ggModel, imLowRate, imNormalRate, imTempRate, imTaxRate, exReturnRate, exNormalRate, exTempRate, exSpecialRate);
}
重写的时候需要选自己需要的比较的字段
idea有自己生成的办法
生成的时候选择自己需要的字段就可以了
如果你喜欢用stream比较请参考一下方法,用stream显得比较高大上,所以我一直在学习。
List<HjHsCode> oldHsCode, List<HjHsCode> hsCodes
// 差集 (oldHsCode- hsCodes) 就是别人接口删除了,数据需要删除的。
List<HjHsCode> reduce1 = oldHsCode.parallelStream().filter(item ->!hsCodes.contains(item)).collect(Collectors.toList());
// 差集 (新数据和老数据对比)------------服务器数据和本地数据不一致的,需要修改或者新增的 (hsCodes-oldHsCode)
List<HjHsCode> reduce = hsCodes.parallelStream().filter(item -> !oldHsCode.contains(item)).collect(Collectors.toList());
contains 会自动调用重写的equals