hashcode contract

a theoretical question

Page 1 of 1

4 Replies - 4067 Views - Last Post: 20 February 2008 - 06:46 AM Rate Topic: -----

#1 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • 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  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: hashcode contract

Posted 20 February 2008 - 04:50 AM

View Postbhandari, 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?



can not help you till you post the code. :D

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.
Was This Post Helpful? 0
  • +
  • -

#3 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,634
  • Joined: 16-October 07

Re: hashcode contract

Posted 20 February 2008 - 05:51 AM

View Postbhandari, 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.
Was This Post Helpful? 0
  • +
  • -

#5 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1