首页 > 编程语言 > 详细

java IdentityHashMap 与HashMap

时间:2014-10-19 17:06:18      阅读:352      评论:0      收藏:0      [点我收藏+]
这两个map的主要区别在于比较key值的时候:
IdentityHashMap认为当k1 == k2 时key值是一样的
HaspMap认为k1 == null ? k2 == null:k1.equals(k2)时key值是一样的

举个例子:
Integer a = new Integer(123456);
           Integer b = new Integer(123456);
           HashMap hashMap = new HashMap();
           IdentityHashMap identityHashMap = new IdentityHashMap();
           hashMap.put(a,1);
           hashMap.put(b, 2);
           identityHashMap.put(a,1);
           identityHashMap.put(b,2);
           System.out.println(hashMap);
           System.out.println(identityHashMap);

运行结果:
P_LOG: {123456=2}
P_LOG: {123456=1, 123456=2}

总结:
          HashMap:会使用equals比较key对象
          IdentityHashMap:使用 == 比较key对象
      



java IdentityHashMap 与HashMap

原文:http://blog.csdn.net/wangjuntytl/article/details/40263805

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