• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode and equals

 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

In the test CD of K&B book, there is one example:




One of the answers in the CD is



I wonder if this is correct? Because according the the contract between equals and hashCode, "but equals() and hashCode() are bound together by a joint contract that specifies if two objects are considered equal using the equals() method, then they must have identical hashcode values."


In this case, if we have 2 object A and B. Supporse that they have the same code, but A.bal = 2, A.rate = 3, B.bal = 3, B.rate = 2. In this case, equals() return true but these objects do not have the same hashCode, which violate the contract.

Is this answer correct???
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we have 2 object A and B. Supporse that they have the same code, but A.bal = 2, A.rate = 3, B.bal = 3, B.rate = 2. In this case, equals() return true but these objects do not have the same hashCode, which violate the contract.

Yes, Correct.
Is there anything else mentioned in the question? Have you checked the errata whether anything related to this presents?
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct. Also given equals() method do not satisfy one of the contract point
For any non-null reference value x, x.equals(null) should return false.


Below mentioned is my analysis



Output is

HashCode Contract
1. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
A.hashCode() 16
A.hashCode() 16
B.hashCode() 24
B.hashCode() 24

2. •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.
A.hashCode() = 16
B.hashCode() = 24
A.equals(B) = true

3. •It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables
A.equals(B) = false
A.hashCode() = 48
B.hashCode() = 24



Equals Contract
1. •It is reflexive: for any non-null reference value x, x.equals(x) should return true.
true
true

2. •It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true
A.equals(B) - true
B.equals(A) - true

3. •It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
A.equals(B) - true
B.equals(C) - true
C.equals(A) - true

4. •It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified
B.equals(A) - true
B.equals(A) - true
B.equals(A) - true

5. •For any non-null reference value x, x.equals(null) should return false.
Exception in thread "main" java.lang.NullPointerException
at aaa.SortOf.equals(SortOf.java:12)
at aaa.SortOf.main(SortOf.java:95)
 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank all of you for your replies
 
sakthi moorthy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3 It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

Does that mean if two objects are unequal (a.equals(b) returns false) then a.hashCode() and b.hashCode() may return same integer results ?

i am confused with the usage of word "not".
Correct me if i am wrong.
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sakthi moorthy wrote:3 It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

Does that mean if two objects are unequal (a.equals(b) returns false) then a.hashCode() and b.hashCode() may return same integer results ?

Yes, it is not required to return the same hash code unless the objects are equal (by equals() method).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic