public class Test {
public static void main(String[] args) {
HashSet l=new HashSet();
Students s1=new Students("zhang1",12);
Students s2=new Students("zhang1",11);
System.out.println(s1==s2);
l.add(s1);
l.add(s2);
Iterator it=l.iterator();
System.out.println();
while(it.hasNext()){
Students p=(Students)it.next();
System.out.println(p.getName()+"..."+p.getAge());
}
System.out.println(s1);
System.out.println(s2);
}
}
public class Students {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Students(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int hashCode() {
System.out.println("hashcode running...");
return name.hashCode();
}
public boolean equals(Object obj) {
Students s=(Students)obj;
System.out.println(this);
return this.name==s.name && this.age==s.age;
}
}
输出结果:
false
hashcode running...
hashcode running...
hashcode running...
Test.Students@d61689c5
zhang1...11
zhang1...12
hashcode running...
Test.Students@d61689c5
hashcode running...
Test.Students@d61689c5
问题1:Test类中最后两句话打印s1和s2的地址,结果都是
Test.Students@d61689c5,为什么地址会一样?可是通过s1==s2发现地址是不一样的。
问题2:输出对象的时候,为什么会调用hashcode方法?
Test类中最后两句话打印s1和s2的地址
这个打印的不是地址,而是hash值。hash相同未必就是同一个对象。
因为默认的toString()就是输出hashcode,所以第二个问题没有回答的必要了。
你没有重写toString方法,用的是Object的toString,以下是Object的toString方法源码,你看就明白了
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
输出的Test.Students@d61689c5并不是类的地址,而是类名@hashcode,两个Student实例内容是一样的,根据你重写的hashCode方法,相同内容的String会生成相同的hashCode,不难理解为什么输出相同的Test.Students@d61689c5了
问题1:你看看System.out.println(s1)的源码就知道原因了
//println的源码里面有个 String.valueOf(x)
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
//再来看看 String.valueOf(x)的源码
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
//看到没最后打印实体类会调用obj.toString()这个方法
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
//toString()打印的是实体类名和hashCode的值,实例内容一样,所以hashCode 也一样
问题2:因为你重新了hashCode 和 equals方法,所以输出的时候会调用这2个方法