Always override Object.equals() and Object.hashCode() in the same time

来源:互联网 发布:java自学的方法 编辑:程序博客网 时间:2024/04/29 10:41
Always override Object.equals() and Object.hashCode() in the same time. I had met with such bugs serveral times and it was a hard time for me to figure out such bug.

Quotes:
http://www-106.ibm.com/developerworks/library/j-jtp06294.html
Hashing it out
In "Hashing it out," I outlined the basic rules for correctly overriding Object.equals() and Object.hashCode(), in particular that equal objects (according to equals()) must have equal hashCode() values. While this rule is fairly easy to follow once you know it (and some IDEs have wizards for defining both for you in a consistent manner), if you override one of these methods and forget to override the other, this bug can be very difficult to detect through inspection -- because the error is not in the code that is present, but in the code that is absent.
原创粉丝点击