hashcode contract

Page 1 of 1

4 Replies - 4373 Views - Last Post: 20 February 2008 - 06:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=43816&amp;s=33e0075c1973cd54a8fff774ad7223cb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 bhandari

Reputation: 9
• Posts: 754
• Joined: 31-January 08

hashcode contract

Posted 20 February 2008 - 04: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?

This post has been edited by bhandari: 20 February 2008 - 04:53 AM

Is This A Good Question/Topic? 0

Replies To: hashcode contract

#2 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

Re: hashcode contract

Posted 20 February 2008 - 04:50 AM

bhandari, on 20 Feb, 2008 - 04:35 PM, said:

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?

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.

#3 bhandari

Reputation: 9
• Posts: 754
• Joined: 31-January 08

Re: hashcode contract

Posted 20 February 2008 - 05:02 AM

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 February 2008 - 05:03 AM

#4 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

Re: hashcode contract

Posted 20 February 2008 - 05:51 AM

bhandari, on 20 Feb, 2008 - 06:05 AM, said:

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.

#5 bhandari

Reputation: 9
• Posts: 754
• Joined: 31-January 08

Re: hashcode contract

Posted 20 February 2008 - 06:46 AM

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