Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

HashMap Keys?

Thread view: 
Ken - 02 Jul 2004 03:51 GMT
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
GG - 14 Jul 2004 08:00 GMT
> Hi.  I'm looking for advice on using keys for a HashMap.

It all depends on what you're trying to do, duh :). First of all, check out
how the HashMap works at all, the source is available. See if you understand
the requirements for equals() / hashCode(). See if you can follow why
Strings are common choice (well, also because it's just easy :). But maybe
that's not what you want. What problem are you trying to solve?
Jonni Gani - 16 Jul 2004 10:57 GMT
> 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.

Don't worry too much whether using String is more efficient (generally
it's bad to worry about optimization during implementation). You
should use whatever object which makes the best sense in your
situation. Note that Map keys must be *immutable*, otherwise the Map
may not work properly. Strings are immutable.
Adam Maass - 20 Jul 2005 05:39 GMT
>> 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
[quoted text clipped - 6 lines]
> situation. Note that Map keys must be *immutable*, otherwise the Map
> may not work properly. Strings are immutable.

Not quite. Objects used as keys in a Map must not change while they are in
use as keys in a Map. Immutable objects never change once constructed, so
they meet this requirement.

-- Adam
Stephen Curtin - 22 Jul 2004 16:36 GMT
> Hi.  I'm looking for advice on using keys for a HashMap.
>
[quoted text clipped - 6 lines]
>
> Ken

Hi Ken

I always use Strings as keys in a hash map and I have never
encountered problems in either effiency or effectiveness

Steve


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.