java中IdentityHashMap和HashMap区别说明
下文笔者讲述IdentityHashMap和HashMap的区别说明,如下所示:
IdentityHashMap和HashMap的区别: IdentityHashMap:当k1 == k2时key值是一样的 HaspMap:k1 == null ? k2 == null:k1.equals(k2)时key值是一样 可总结为: HashMap:会使用equals比较key对象 IdentityHashMap:使用 == 比较key对象例:
Integer a = new Integer(88888);
Integer b = new Integer(88888);
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: {88888=2}
P_LOG: {88888=1, 88888=2}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


