重写Java 的equals和hashcode方法的时候应该注意什么

equals() (javadoc) must define an equivalence relation (it must be reflexivesymmetric, 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.html

hashCode() (javadoc) must also be consistent (if the object is not modified in terms of equals(), it must keep returning the same value).java

The relation between the two methods is:api

Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().oracle

equals 方法和hashcode方法的关系如上。当对象a等于对象b 那么他们的hashcode方法值也必定相同。flex

相关文章
相关标签/搜索