散列碼(hashcode)

来源:互联网 发布:做淘宝快递单怎么打印 编辑:程序博客网 时间:2024/05/09 18:22

 1.在散列(hashing)中,一個關鍵字的信息內容被用來確定唯一的一個值,稱位散列碼(hashcode)

 2.1. Rule: If the equals() method returns true, then hashCode() for both the objects must return the same value. Note that the reverse is not required. So, it is ok if the equals() method returns false but the hashCode() returns same value for both the objects.

2.2. The hasCode() method does not satisfy the requirement:
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
For example, equals() will return true for: new Info("aa", "b", "c") and new Info("a", "ab", "c") but their hashCode() values will be different.

2.3 So we must overiding equals() and hascode() in bean ,assure the two object is equals

原创粉丝点击