Java中key值可以重復(fù)的map
在正常的Map 實(shí)現(xiàn)(如 HashMap)中亏吝,當(dāng)且僅當(dāng)滿足下列條件時(shí)才認(rèn)為兩個(gè)鍵 k1 和 k2 相等:(k1==null ? k2==null : e1.equals(e2))篇恒;
而 IdentityHashMap 類利用哈希表實(shí)現(xiàn) Map 接口义屏,比較鍵(和值)時(shí)使用引用相等性(比較的是在內(nèi)存中的儲(chǔ)存地址)代替對(duì)象相等性默辨;
IdentityHashMap<String,Object> map =newIdentityHashMap<String,Object>();
map.put(newString("xx"),"first");
map.put(newString("xx"),"second");
for (Entry<String, Object> entry : map.entrySet()) {
System.out.print(entry.getKey() +" ");
System.out.println(entry.getValue());
}
System.out.println("idenMap="+map.containsKey("xx"));
System.out.println("idenMap="+map.get("xx"));
xx first
xx second
idenMap=false
idenMap=null