使用引用相等的集合 [英] Collection using reference equality
本文介绍了使用引用相等的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中是否可以创建使用引用相等性(即 ==
)而不是 equals()
方法的 HashMap
?
In Java is it possible to create HashMap
that uses reference equality (i.e. ==
) instead of the equals()
method?
推荐答案
使用 IdentityHashMap
类.这是 HashMap
的一种变体,其中使用 ==
和 System.identityHashCode()
代替了 Object.equals(Object)
和 Object.hashCode()
.
Use the IdentityHashMap
class. This is a variant of HashMap
in which ==
and System.identityHashCode()
are used instead of Object.equals(Object)
and Object.hashCode()
.
请注意,此类故意违反了要求的 java.util.Map
密钥相等性基于 equals(Object)
.
Note that this class intentionally violates the API contract of java.util.Map
which requires that key equality is based on equals(Object)
.
这篇关于使用引用相等的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文