This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).)
# 1 楼答案
使用^{} 类。这是
HashMap
的一个变体,其中==
和System.identityHashCode()
被用来代替Object.equals(Object)
和Object.hashCode()
请注意,此类故意违反了^{} 的API约定,该约定要求密钥相等是基于
equals(Object)
# 2 楼答案
可以重写插入HashMap的对象的equals方法来测试引用相等性
例如:
# 3 楼答案
IdentityHashmap类附带标准Java。从JavaDoc:
请注意,许多采用
Map
的函数都假定它们将使用equals
,而不是引用相等。因此,要小心将IdentityHashmap
传递给哪些函数