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 ]
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 ?
PS. Yes - Joshua's Bloch book is at the top of my next to read list
All that thinking. Doesn't it hurt? What do you think about this tiny ad?