Sagewire Logo

HashMap Keys?

5 Message(s) by 5 Author(s) originally posted in java developer


From: kk_oop Date:   Thursday, July 01, 2004
Hi. I'm looking for advice on using key s 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


From: grisha Date:   Wednesday, July 14, 2004
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() / hash Code(). 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?


From: giffy_jif Date:   Friday, July 16, 2004
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.



Do not 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.


From: curtinsteve Date:   Thursday, July 22, 2004
wrote in message
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



Hi Ken

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

Steve


From: Adam Maass Date:   Wednesday, July 20, 2005
wrote in message:
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.
Do not 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.



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



Next Message: How I can send text to printer?


Blogs related to HashMap Keys?

Can Map Do A Better Job at Allowing Optimized Iteration Over Its ...
The keys iterator returns the keys so it has to walk the keys, however the get(key) has to lookup the key each time.Internally implementations like HashMap store keys and values as linked list pairs.

time based cache
import java.util.Map; import java.util.WeakHashMap; ... @param key unique identifier to retrieve object ... public synchronized Object get(Object key, Callback block) { ... import java.util.HashMap; import junit.framework.TestCase; ...

Problem using dynamic variable as HashMap key
But when I load my variable value from my data like this... ... reflects the data obtained from my query: ... I have determined that the hashmap key must be a string because if I change ...

How do I use an Oracle Ref Cursor? (created)
This FAQ is based on iBATIS DataMapper (Java) 2.2.0.638 ... CREATE TABLE REFS ( ID NUMBER NOT NULL PRIMARY KEY , NAME VARCHAR2(50) NOT NULL ); CREATE OR REPLACE ... HashMap; import java.util.Map; import com.ibatis.common.resources. ...

Top 10 Java Classes I Love to Hate
Here are ten Java classes in the standard API that annoy me whenever I ... Weak keys? Weak values? It turns out that it's weak keys, but it would be ... Since it is a weak key hashmap it should actually be a weak key identity hashmap. ...

In Favor of Strong Typing in CFML...an Illustrated Example
Things immediately get a little tricky, however, because HashMap keys are ... One way to solve this is to always convert they keys to uppercase, ... Now that we have our two utility methods, let's convert the CFIF to Java: ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional