Welcome to Dream.In.Code
Become a Java Expert!

Join 150,375 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,620 people online right now. Registration is fast and FREE... Join Now!




hashcode contract

 
Reply to this topicStart new topic

hashcode contract, a theoretical question

bhandari
20 Feb, 2008 - 03:05 AM
Post #1

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
If equals() returns true then hashcode comparison should also return true.
If equals() returns false then hashcode comparison may return true or false.
If hashcode comparison returns true then equals() may or may not return true.
If hashcode comparison returns false then equals() must return false.


I am unable to understand why the fourth is must staement. Any ideas?

This post has been edited by bhandari: 20 Feb, 2008 - 03:53 AM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Hashcode Contract
20 Feb, 2008 - 03:50 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,167



Thanked: 45 times
Dream Kudos: 125
My Contributions
QUOTE(bhandari @ 20 Feb, 2008 - 04:35 PM) *

If equals() returns true then hashcode comparison should also return true.
If equals() returns false then hashcode comparison may return true or false.
If hashcode comparison returns true then equals() may or may not return true.
If hashcode comparison returns false then equals() must return false.


I am unable to understand why the fourth is must staement. Any ideas?



can not help you till you post the code. biggrin.gif

Well I think it's bit obvious statement but it's there to show the vice versa situation.
2nd and 3rd statements shows that vice versa may or may not true, so 4th statement is there to tell that vice versa statements are always false.

I mean this is the first thought I had on this. There might be some other logic also.
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Hashcode Contract
20 Feb, 2008 - 04:02 AM
Post #3

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
my dilema is that equals() can check for anything (any attribute) and that shouldn't be a point for associating equals() with hashcode().
how does hashcode associates to equals at all?

This post has been edited by bhandari: 20 Feb, 2008 - 04:03 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Hashcode Contract
20 Feb, 2008 - 04:51 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(bhandari @ 20 Feb, 2008 - 06:05 AM) *

If equals() returns true then hashcode comparison should also return true.
If equals() returns false then hashcode comparison may return true or false.
If hashcode comparison returns true then equals() may or may not return true.
If hashcode comparison returns false then equals() must return false.


I am unable to understand why the fourth is must staement. Any ideas?


The last line is more an assertion than an implementation requirement.
If you think of the first statement as "If ConditionA then ConditionB", the fourth statement is "If not ConditionB then not ConditionA".

Implement the first and the fourth will follow.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

bhandari
RE: Hashcode Contract
20 Feb, 2008 - 05:46 AM
Post #5

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
thanks @baagvi,

I tried to modify the 4th statement as:
If hashcode comparison returns false then equals() must return true.

which contradicts 1st statement and 1st statement is a mandate.

thanks again

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month