首页 > 其他 > 详细

重写equal要重写 hashCode的原因

时间:2016-02-24 21:06:54      阅读:184      评论:0      收藏:0      [点我收藏+]
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

重写equal要重写 hashCode的原因

原文:http://www.cnblogs.com/qiumingcheng/p/5215063.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!