public static void main(String[] args) {
IdentityHashMap<Integer, Object> m1 = new IdentityHashMap<Integer, Object>();
Integer ONE = 1;
Integer TWO = 2;
Integer OTHER_ONE = new Integer(1);
Integer OTHER_TWO = new Integer(2);
m1.put(ONE, new Object());
m1.put(TWO, new Object());
System.out.println(m1.keySet()); // [1, 2]
m1.remove(OTHER_ONE); // Does not remove
System.out.println(m1.keySet()); // [1, 2]
m1.remove(ONE); // Does remove
System.out.println(m1.keySet()); // [2]
m1.keySet().removeAll(Arrays.asList(OTHER_TWO)); // ...
System.out.println(m1.keySet()); // [] WHAT?
}
這裏IdentityHashMap.keySet()爲什麼IdentityHashMap.keySet()。removeAll(keys)不使用標識?這是一個錯誤嗎?
參考,我發現在源代碼中的答案(見下文),但我不知道的根本原因。這是一個錯誤嗎?
請參閱http://stackoverflow.com/questions/2021374/boxed-primitives-and-equivalence/2021442。 – Danstahr
Danstahr我知道。我有意使用新的Integer()來測試地圖的身份行爲。我在詢問removeAll。問題不清楚嗎?謝謝。 – aalku