In Java is it possible to create HashMap
that uses reference equality (i.e. ==
) instead of the equals()
method?
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()
.
Note that this class intentionally violates the API contract of java.util.Map
which requires that key equality is based on equals(Object)
.