这是我实现的一部分
aFList = new HashSet<Fault>();
bFList = new HashSet<Fault>();
oFList = new HashSet<Fault>();
temp.addAll(aFList);
temp.addAll(bFList);
oFList.addAll(temp);
故障分类如下
public class Fault {
int nodeIndex;
boolean val;
}
Display() {
Object [] temp;
temp = aFList2.toArray();
for(int i=0;i<temp.length;i++)
((Fault) temp[i]).display();
}
结果:
14_true
6_true
17_false
16_false
16_false
9_false
14_true
问题:为什么我会得到重复的列表元素?可能是因为 oFList 已经包含这些元素,但我认为 HashSet
会处理重复项。我错过了什么吗?
最佳答案
如果您要将类放入 HashMap 或 HashSet 中,则必须实现 equals() 和 hashCode()。
关于java - 奇怪的 HashSet 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7869664/