Re: What to use as HashMap Key?
From: Hemal Pandya (hemalpandya-nospam_at_gmail.com)
Date: 07/06/04
- Next message: Roedy Green: "Re: Roedy Green..."
- Previous message: John Davison: "Extracting CDATA Text without CDATA Tags???"
- In reply to: Lāʻie Techie: "Re: What to use as HashMap Key?"
- Next in thread: Harald Kirsch: "Re: What to use as HashMap Key?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
>> efficient/effective.
>>
>> Thanks for any advice!
>>
>> Ken
>
> 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.
> HTH,
> La'ie Techie
- Next message: Roedy Green: "Re: Roedy Green..."
- Previous message: John Davison: "Extracting CDATA Text without CDATA Tags???"
- In reply to: Lāʻie Techie: "Re: What to use as HashMap Key?"
- Next in thread: Harald Kirsch: "Re: What to use as HashMap Key?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|