Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashcode and equals

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a specific way to find out which is the appropriate implementation of hashcode together with equals method among all the legal implementation.


 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the way I check this:
For a hashCode() implementation to be appropriate (or correct) you need to make sure that for two objects a and b, if a.equals(b) is true, then a.hashCode() == b.hashCode().
For a hashCode() implementation to be legal you just need to provide a valid override of the hashCode method in Object. But just because it's legal it doesn't mean it's appropriate. For it to be appropriate (or correct) it needs to be legal, plus it needs to satisfy the contract with equals.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any rule like ...... the instance member that has to be use or instance members that cannot be used or the operation that cannot be performed in the hashode ??
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, hashCode() needs to be less specific than equals. For example, hashCode could return a fixed value for every object of the class and that would be OK. The opposite is not true. If you see hashCode() using an instance or static member (either field or method) that is not used by equals() that should raise a red flag, because it means that hashCode() could return different values for two objects that are equal (according to equals().) But you should always think the question through, and ask yourself how it would be possible for a and b to have different hashCodes yet be equal. If you find any two of such objects then the implementation of hashCode() is not correct (not correct with respect to the current implementation of equals() anyway.)
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. I think i understood . But you know its still kind off blur...
I think it will clear once i start judging the answers based on the above rules...
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you mean.
There is a single rule to see if a hashCode() implementation is appropriate: For any two objects, if the objects are equal according to equals(), then their hashcodes given by hashCode() must be the same value.

You really can't take shortcuts unfortunately. What I said earlier about hashCode() using members of the class in its computations that are not used in equals() does not always work. Take a look at this example:

Here, equals() is using only the instance variable a, but hashCode() is using b (which equals() doesn't use.) However, hashCode() is a correct implementation, because equals() will always return false, so no matter what hashCode does, as long as it is legal, it will be correct.

This means that you always need to read through the code and see what's going on. No fast and easy rules.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically we have to judge based on the current suitation ...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Tharakan wrote:So basically we have to judge based on the current suitation ...


Yes, and always discard hashCode(), if they are using Math.random() function to determine hashcode value.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is legal but not appropriate, right??
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi vijay wrote:It is legal but not appropriate, right??


Yes, I think.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Singh wrote:
Abhi vijay wrote:It is legal but not appropriate, right??


Yes, I think.

I think so too. For hashCode() legal means that it's a valid override. Appropriate means that it also abides by the contract.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Singh wrote:
James Tharakan wrote:So basically we have to judge based on the current suitation ...


Yes, and always discard hashCode(), if they are using Math.random() function to determine hashcode value.

That's right, Punit. But I think there is an exception: If equals() always returns false, then hashCode() will be appropriate no matter what, as long as it is a valid override. It could even have Math.random() and it wouldn't break the contract, because it could only break the contract in cases where equals() returns true.
EDIT: I realized that what I said is wrong, since the return of hashCode() must be consistent if the state of the object doesn't change. It would violate its own contract, but not the contract between equals() and hashCode().
 
Piyush Porwal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you are looking for is a set of test cases, which can be executed on any class and gives a red or green signal.
Junit is designed just for that. I doubt though that it could be of much help for SCJP. Few links that I found on googling:

JUnit built in class to work on it:
http://junit-addons.sourceforge.net/junitx/extensions/EqualsHashCodeTestCase.html

Good explanation on how to write a junit test cases by yourself:
http://www.ibm.com/developerworks/java/library/j-unitx/

Another one:
http://stackoverflow.com/questions/188311/how-should-one-unit-test-the-hashcode-equals-contract

Hope that helps. Let me know if you really implement any of these options for your custom class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic