重寫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

相關文章
相關標籤/搜索