• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

equals() and hashCode() contract

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Just a thought:

This violates the equals and hashCode contract but it
works perfectly well. If you use it like this:

you still are able to put these two objects into a map
and still are able to retrieve them.

My question is: if you are, what is this contract for ?
I mean if it works, why do we need to follow this contract ?
Are there any exceptions (some examples where it WON'T work) ?

And secondly - why equals needs to be symmetric and transitive ?

Cheers,

Adrian
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you still are able to put these two objects into a map
and still are able to retrieve them.



Really?!? According to the definition of a HashMap, duplicates are not allowed -- and since those two objects are equal, didn't you just break the HashMap? (as it doesn't detect the duplicate?) .... IOWs, your equal() / hashCode() contract is broken, which makes your HashMap behave in a way that it shouldn't, and you think that this is good?

And secondly - why equals needs to be symmetric and transitive ?



If the equals were not so, then depending on the order that you place objects into the HashMap... it will be treated as a duplicate or not (based on the order).

Henry
[ July 16, 2007: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Bloch's Effective Java items 7 & 8.

Very good book on Java in general.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adrian

try putting and getting this also along with i1 and i2:



I think then it will not put this key into HashMap.

Murali...
 
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Thanks for your reply !

I just woke up this morning and said to myself - what
am I writing ? I have just totally forgotten that
it SHOULDN'T add the other object to the map ...
Ehhh - sometime it's better to take a brake rather
then forcing yourself to finish something

As for symmetric and transitive - isn't the first one
results from the second one ? I mean if A equals B
and B equals C, then A equals C - that's quite
straightforward ... But if they weren't symmetric,
that would brake the transitive rule. Am I right ?

Cheers,

Adrian

PS. Yes - Joshua's Bloch book is at the top of
my next to read list
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic