Re: NSDictionary, keys, and equals
Re: NSDictionary, keys, and equals
- Subject: Re: NSDictionary, keys, and equals
- From: Adam Ramadan <email@hidden>
- Date: Thu, 26 Feb 2004 10:10:31 -0500
The best explanation I have seen of how to correctly implement
equals/hashcode is in Sun's "Effective Java", chapter 3. Fortunately,
Sun makes several sample chapters, including chapter 3, available on the
web:
http://java.sun.com/docs/books/effective/chapters.html
Enjoy,
Adam
Art Isbell wrote:
On Feb 25, 2004, at 2:24 PM, Jonathan Rochkind wrote:
I have implemented a custom equals() on that custom object class.
(Don't worry, it's not an EO).
My impression was, if I stored an object in an NSMutableDictionary
under a certain key, it would be retrievable not only by that exact
same object (identity ==) as a key, but by _another_ object of the
same type that equals() the first object, even if it doens't == the
first object.
However, this does not seem to be the case. What's up with that?
Is it the mysterious hashCode()? Cause I haven't actually provided a
custom implementation of hashCode(), I'm just using whatever
java.lang.Object gives me. Is this the problem?
That would be my guess because the method description for
Object.hashCode() states:
"As much as is reasonably practical, the hashCode method defined by
class Object does return distinct integers for distinct objects. (This
is typically implemented by converting the internal address of the
object into an integer, but this implementation technique is not
required by the JavaTM programming language.)"
So where object1 == object2, equals() is true and hashCode()
returns the same value. But where object1.equals(object2) is true and
object1 == object2 is false, hashCode() will return different values
for each object which may cause NSDictionary to consider the keys to
be different.
Aloha,
Art
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.