File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  hashcode and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " hashcode and equals" Watch " hashcode and equals" New topic
Author

hashcode and equals

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Is there a specific way to find out which is the appropriate implementation of hashcode together with equals method among all the legal implementation.



SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

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

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Aug 29, 2008
Posts: 580

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

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Aug 29, 2008
Posts: 580

So basically we have to judge based on the current suitation ...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.


SCJP 6
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
It is legal but not appropriate, right??
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:It is legal but not appropriate, right??


Yes, I think.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Apr 09, 2008
Posts: 30
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.


-Piyush Porwal (Junk box)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode and equals
 
Similar Threads
Interview question
Duplicates in HashSet
Doubt in hashCode()
how to solve this hashcode() example.?
hashCode() implementation