重写equal要重写 hashCode的原因

public class Test {

public static void main(String[] args) {

Person person1 = new Person();
person1.setId("1");
person1.setName("qiumc");

Person person2 = new Person();
person2.setId("1");
person2.setName("qiumc");

Map<Person, String> hashMap = new HashMap<Person, String>();
hashMap.put(person1,"1");
hashMap.put(person2,"2");

System.out.println(hashMap.size());//重写hashCode 结果为1,否则为2

}


}


//Person类省略,只有两个属性,一个是name,一个id
posted @ 2016-02-24 20:22  邱明成  阅读(359)  评论(0编辑  收藏  举报