equals()
(javadoc) must define an equivalence relation (it must be reflexive, symmetric, andtransitive). In addition, it must be consistent (if the objects are not modified, then it must keep returning the same value). Furthermore, o.equals(null)
must always return false.
hashCode()
(javadoc) must also be consistent (if the object is not modified in terms of equals()
, it must keep returning the same value).
The relation between the two methods is:
Whenever
a.equals(b)
, thena.hashCode()
must be same asb.hashCode()
.
equals 方法和hashcode方法的关系如上。当对象a等于对象b 那么他们的hashcode方法值也一定相同。