Re: What to use as HashMap Key?

From: Hemal Pandya (hemalpandya-nospam_at_gmail.com)
Date: 07/06/04


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



Relevant Pages

  • Re: best way to get translation jobs
    ... advice and tips on how to get started. ... Sometimes agencies are asking for translators in the papers. ... Also, I got some work through Proz.com, although I'm not a paying member. ... other Proz can ...
    (sci.lang.translation)
  • Re: Music & the Spoken Word
    ... holy are advised not to watch television. ... I've only been a member for 35 years, but I've never run up against ... that advice. ... "If you want to invade large swathes of Europe and Asia with an army of=20 ...
    (soc.religion.mormon)
  • Re: The Record Collector/UK
    ... I would suggest writing directly to Larry Lustig and asking his advice. ... other than as an avid subscriber and member of the Recorded ... Vocal Art Society of London, a society that meets fortnightly to hear a ...
    (rec.music.opera)
  • sony protector error
    ... my sony str-d1015 receiver goes into protect mode. ... checked all speaker connections and all checked out ok. ... advice of another member who had the same problem and found part # ...
    (sci.electronics.repair)
  • building an object index - data structure question
    ... (constructed from a database resultset) ... member variables. ... one for each variable member I want to include in the ... with the hashmap key being the member value and the hashmap ...
    (comp.lang.java.programmer)