我使用eclipse生成的equals和hashCode后,结果向hashMap中存储成员值相同的键依旧可以存入,很奇怪
public int hashCode() { //年龄相同的hashCode相同
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + name.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;
Student1 other = (Student1) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true; //姓名相同年龄相同返回true
}