【发布时间】:2019-06-03 17:41:56
【问题描述】:
我的 HashSet 包含多个具有相同 HashCode 的“AccessRequests”。我只希望有一个实例。我认为具有相同 HashCode 的项目不会出现在 HashSet 中。我在这里做错了什么?
更新:基于 HashSet 仅在列表中保留一个不等于另一个项目的假设,并且我的 equals/hash 方法可能需要简化,我已经更新了我的问题。我仍然在我的 HashSet 中获得多个评估为 Equals 的项目。
以下是“AccessRequest”中的 HashCode 和 Equals 方法
更新:我更新了我的哈希和等于,只有我需要“等于”的必要字段
@Override
public int hashCode() {
int hash = 5;
hash = 79 * hash + Objects.hashCode(this.targets);
hash = 79 * hash + Objects.hashCode(this.sources);
hash = 79 * hash + Objects.hashCode(this.destinations);
hash = 79 * hash + Objects.hashCode(this.services);
hash = 79 * hash + Objects.hashCode(this.action);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final AccessRequest other = (AccessRequest) obj;
if (!Objects.equals(this.action, other.action)) {
return false;
}
if (!Objects.equals(this.targets, other.targets)) {
return false;
}
if (!Objects.equals(this.sources, other.sources)) {
return false;
}
if (!Objects.equals(this.destinations, other.destinations)) {
return false;
}
if (!Objects.equals(this.services, other.services)) {
return false;
}
return true;
}
创建 AccessRequest 后,我将它们转储到 HashSet 并迭代: 我的 HashSet 定义如下:
Set<AccessRequest> ars = new HashSet();
ArrayList<AccessRequest> arsAsList = new ArrayList(ars);
for(int position=0;position<arsAsList.size();position++){
AccessRequest fixedAR = arsAsList.get(position);
ArrayList<AccessRequest> comparToList = new ArrayList(ars);
for(int cPosition=0;cPosition<comparToList.size();cPosition++){
AccessRequest nextAR = comparToList.get(cPosition);
if(fixedAR.equals(nextAR)){
System.out.println("position= "+position+" cPosition "+cPosition);
}
}
System.out.println("\n Next AR");
}
以下是输出:
position= 0 cPosition 0
position= 0 cPosition 5
position= 0 cPosition 6
position= 0 cPosition 14
position= 0 cPosition 24
position= 0 cPosition 32
position= 0 cPosition 39
position= 0 cPosition 40
position= 0 cPosition 43
position= 0 cPosition 77
position= 0 cPosition 96
position= 0 cPosition 97
position= 0 cPosition 99
position= 0 cPosition 109
position= 0 cPosition 111
position= 0 cPosition 115
position= 0 cPosition 173
position= 0 cPosition 182
position= 0 cPosition 187
【问题讨论】:
-
是的,它允许有哈希码冲突,哈希表也会检查键是否相等。
-
您在这里使用了大量的字段来为不相等的对象计算相同的哈希码(4 个不相等的对象具有相同的哈希码!)。每个字段是否有真正有用的 hashCode 实现?