该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
/*求教我都已经改写作为键的Student对象的equals()和hashCode()函数了,为何用get()方法还是检索不到对应的value值呢?
*/
import java.util.*;
class Student
{
private String name;
private int age;
private String school;
public Student(String name,int age,String school)
{
this.name=name;
this.age=age;
this.school=school;
}
public boolean equals(Student obj)
{
if((name.equals(obj.name)) & (age==obj.age) & (school.equals(obj.school)))
return true;
else return false;
}
public int hashCode()
{
return(name.hashCode()+school.hashCode()+age);
}
}
public class THashCode
{
public static void main(String []args)
{
Hashtable ha=new Hashtable();//键为Student类,值为Integer类
Student s1=new Student("aaa",18,"bupta");
Student s2=new Student("bbb",21,"buptb");
Student s3=new Student("ccc",23,"buptc");
ha.put(s1,new Integer(321));
ha.put(s2,new Integer(322));
ha.put(s3,new Integer(321));
System.out.println(ha.get(s1));//可找到321
Student s=new Student("aaa",18,"bupta");
System.out.println("表中键的hashCode为:"+s1.hashCode());
System.out.println("提供键的hashCode为"+s.hashCode());
System.out.println("两个key相同是"+s1.equals(s)+"的");
System.out.println(ha.get(s)); //???
}
}