wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode() terms confusion in K&B SCJP 6 book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode() terms confusion in K&B SCJP 6 book" Watch "hashCode() terms confusion in K&B SCJP 6 book" New topic

hashCode() terms confusion in K&B SCJP 6 book

Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
I am a little confused over the hashCode() terminology in K&B SCJP 6 book, i.e. appropriate, correct, legal, and efficient of hashCode().

In page 550, it says,"You must also be able to recognize an appropriate or correct implementation of hashCode(). This does not mean legal and does not even mean efficient......So for the exam, if you're asked to pick out an appropriate or correct use of hashcode, don't mistake appropriate for legal or efficient."
(From the above, appropriate != legal or efficient)

In page 553,"A hashCode() that returns the same value for all instances whether they're equal or not is still a legal -- even appropriate -- hashCode() method! For example,
public int hashCode() { return 1492;}
(From the above, appropriate seems the same as legal)

In page 631, "An appropriate hashCode() override sticks to the hashCode() contract.". "An efficient hashCode() override distributes keys evenly across its buckets.
(From the above, appropriate seems like legal)

What is "appropriate"?
Can someone show me an example of hashCode() that is legal, but not appropriate, or correct?

"There are no dumb questions" quoted from HFSJ. "To err is human; to forgive is divine"
Ameen Imtiaz

Joined: Mar 18, 2013
Posts: 7

means correct syntax. compiles successfully. Follows the override rules. Also, hashCode should override and not overload. All this will make it Legal and correct hashCode().

Fullfills the contracts. i.e. two equal objects have equal hashcodes.

Has to do with the algorithm / formula used to return the value. Should be distributed as evenly as possible.


1) Below code shows illegal hashcode. Has incorrect override

2) Below code shows legal and appropriate implementation of hashCode() as it fulfills the contract. Although its highly inefficient.

3) Below code shows legal BUT inappropriate implementation as it does not fulfill the contract. The equal() is based on Person's name. So two objects with same name are "meaningfully equivalent" BUT the hashcode() will not always be same as its is based on age. So the hashcode() does not fulfill the contract which is that if two objects are equal, their hashcodes must be equal as well

To make the above class appropriate for Sets and Maps, we have to change the hashcode() implementation in such a way that when two objects are equal according to the equals(), then the hashCode() of both objects must return same value. Below is one possible solution:

As the String's hashCode() is very efficient, hence we have made our hashCode() implementation LEGAL, Appropriate and also very efficient.

I hope this helps.

Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
Thank you very much, Ameen, for taking time to explain the difference among these terms to me in details. Now I understand that Legal means compiling and running without problem, but it does not have to fulfill contracts, i.e. even if we don't override equal() and hashCode(), it is still legal. Appropriate means Legal plus overriding to fulfill the equals() and hashCode() contracts.
Ameen Imtiaz

Joined: Mar 18, 2013
Posts: 7

Just remember that Appropriate mean to fullfilling the contracts. i.e.

If x.equals(y) is true, x.hashCode() == y.hashCode() MUST be true.

So to know whether the implementation is appropriate, you have to check the code of both equals() and hashCode().
Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
Sure. Thank you again.
I agree. Here's the link: http://aspose.com/file-tools
subject: hashCode() terms confusion in K&B SCJP 6 book
Similar Threads
Clarification needed on K&B use of "legal" and "appropriate" regarding hashCode() implementations
hashCode() method contract
Random question
SCJP doubt about legal and correct/appropriate
variables in hashCode( ) and equals( )