Dir*_*irk 15
如果您正在使用a的实现,java.util.Set
只要您equals
和hashCode
方法正确实现,就不应该允许重复.不知道为什么你的问题上有hashmap和hashtable作为标签.也许你应该改写你的问题并添加给你问题的代码?
编辑:考虑您的编辑:
如果您使用a Set
,您的Employee应该具有以下方法:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)