在我的代码中,我有一组 PlacesInfo 对象,即
Set<PlacesInfo> placeId;
在这个集合中,我添加了 placeId(字符串)。我需要避免向我的 HashSet 添加重复项。这是我下面的覆盖方法。但是,它仍然在我的集合中添加重复的元素。那么,如何避免这种情况呢?
@Override
public int hashCode() {
int hash = 5;
hash = 97 * hash + Objects.hashCode(this.placeId);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return true;
}
if (this.getClass() != obj.getClass()) {
return false;
}
final PlacesInfo other = (PlacesInfo) obj;
if (!Objects.equals(this.placeId, other.placeId)) {
return false;
}
return true;
}