Re: What to use as HashMap Key?
From: Hemal Pandya (hemalpandya-nospam_at_gmail.com)
Date: Tue, 06 Jul 2004 14:16:22 -0700
=?UTF-8?b?TMSByrtpZSBUZWNoaWU=?= <laie@win_remove_get_nospam_solutions.com> writes:
> On Thu, 01 Jul 2004 19:48:05 -0700, Ken wrote:
>> Hi. I'm looking for advice on using keys for a HashMap.
>> Is it typical to just use a string as a key, e.g., "Item A", "Item B",
>> etc.? Or are other approaches better? Since the key is just an Object, I
>> thought there might be some other approach commonly used that is more
>> Thanks for any advice!
> Remember, any Object used as a key should have a fixed hash code and it's
> definition of "equals" should not change throughout its life cycle. This
> is most easily accomplished by using an immutable Object (such as String
> or Integer).
I assume the Object here is instance member. Maybe I am wrong, but how
will this help if that member is itself not final?
When using non-primitive type members, I like to compute the hash
code from hashCode method of final members.
BTW, I believe this is a Big Gotcha. I am surprised that
http://mindprod.com/jgloss/hashcode.html does not address this.
> La'ie Techie