为什么我的equals方法用了也还是返回false 代码如下
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Student))
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public void testContainsKeyOrValue() {
//提示输入学生ID
System.out.println("请输入要查询的学生ID");
Scanner sc=new Scanner(System.in);
String id=sc.next();
//用ContainsKey判断
System.out.println("您输入的学生ID为"+id+"在学生映射表中是否存在"
+students.containsKey(id));
if(students.containsKey(id)) {
System.out.println("对应学生为:"+students.get(id).name);
}
//提示输入学生姓名
System.out.println("请输入要查询的学生姓名");
String name=sc.next();
//用ContainsValue方法来判断是否存在输入的学生姓名
if(students.containsValue(new Student("null","name"))) {
System.out.println("映射表中包含学生"+name);
}else {
System.out.println("该学生不存在");
}
}