gpt4 book ai didi

java - 我应该如何获得 2 个 list 之间的差异
转载 作者:行者123 更新时间:2023-12-02 01:21:01 25 4
gpt4 key购买 nike

有2个不同大小和对象的实体列表,例如List<BrandEntity> baseEntityListList<BrandEntity> subEntityList ,现在我想获取存储在baseEntityList中而不是subEntityList中的结果,不同的维度是brandName。我已经重写了 equals 方法,但它不起作用。这是我的代码。

Main.class: 
findDifferenceList(baseEntityList, subEntityList)
Method:

private <T> List<T> findDifferenceList(List<T> baseBrandList, List<T> subBrandList) {
return baseBrandList.stream().filter(item -> !subBrandList.contains(item)).collect(toList());

}

BrandEntity:

@Slf4j
public class BrandEntity {
@JsonSetter("shopid")
Long shopId;


@JsonSetter("brand")
String brandName;

@JsonIgnore Long principalId;

// getter and setter

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BrandEntity that = (BrandEntity) o;
return Objects.equals(brandName, that.brandName);
}

@Override
public int hashCode() {
return Objects.hash(brandName);
}
}

最佳答案

这是一些棘手的代码,如果我想这样做,我将从 baseEntityList 中删除所有 subEntityList
或者,如果您想查找两个列表中的差异,您可以同时对两个列表进行操作

var diffWithBase = subEntityList.removeAll(baseEntityList);
var diffWithSubList = baseEntityList.removeAll(subEntityList);
// print

关于java - 我应该如何获得 2 个 list<Object> 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57688772/

25 4 0