What to use as HashMap Key?  
Author Message
kk_oop





PostPosted: 2004-7-2 10:48:00 Top

java-programmer, What to use as HashMap Key? 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
 
Chris Smith





PostPosted: 2004-7-2 11:23:00 Top

java-programmer >> What to use as HashMap Key? 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.

There's no answer to the question you asked. HashMaps are used all the
time, and sometimes it's best to use a String as a key and sometimes
it's best to use something different. If I had to guess, I'd say that
String is used more often than any other class, but that still doesn't
make it okay to use String when it's the wrong choice. I certainly
wouldn't go out of my way to use a String when something else is more
appropriate to a specific application.

So, if you want advice, then you'll need to give a specific scenario.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Roedy Green





PostPosted: 2004-7-2 11:28:00 Top

java-programmer >> What to use as HashMap Key? On 1 Jul 2004 19:48:05 -0700, email***@***.com (Ken) wrote or quoted :

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

If you can get away with something more compact or that you can
compare faster, so much the better. If you could scrunch it down to
an dense int, then you would not need a HashMap. You could use an
array.

The most likely common alternative is a Long.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
TMSByrtpZSBUZWNoaWU





PostPosted: 2004-7-2 14:40:00 Top

java-programmer >> What to use as HashMap Key? 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).

HTH,
La'ie Techie

 
 
pifpafpuf





PostPosted: 2004-7-2 16:35:00 Top

java-programmer >> What to use as HashMap Key? email***@***.com (Ken) wrote in message news:<email***@***.com>...
> 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.

Talking about performance, two things enter the equation: how the
hash code of your keys is computed and how fast their
.equals() method is.

Strings:
the hash code is computed by somehow mangling all characters,
.equals() has to compare the two strings character by character.

Integer:
the hash code is the value itself,
.equals() compares the two values with '=='.

Guess yourself which one is faster on the average and then
dismiss performance considerations and first go
for a clean, easy to maintain design when choosing the key
type.

Harald Kirsch
 
 
iamfractal





PostPosted: 2004-7-2 19:21:00 Top

java-programmer >> What to use as HashMap Key? email***@***.com (Ken) wrote in message news:<email***@***.com>...
> 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

I always find it useful to name maps xToY, e.g.,

Map nameToAddress = new HashMap();
Map titleToBook= new HashMap();
Map actionToConsequence = new HashMap();

Although this doesn't help you in your quest of a type of class to use
a key, it helps to show you precisely what your Map is mapping.

Just a thought ...

.ed

www.EdmundKirwan.com - Home of The Fractal Class Composition
 
 
kk_oop





PostPosted: 2004-7-2 19:45:00 Top

java-programmer >> What to use as HashMap Key?

Harald Kirsch wrote:

>
>Guess yourself which one is faster on the average and then
>dismiss performance considerations and first go
>for a clean, easy to maintain design when choosing the key
>type.
>
> Harald Kirsch
>

This was my sense. After I poplulate the HashMap in one class, other
classes will need to access its elements. I figure a String will
provide a good, easy to understand abstraction for each element. In my
case I have a set of rules. So I'd make my key something like "X Factor
Rule" or "Y Factor Rule", where "X Factor" and "Y Factor" are
recognizable names from our domain.

How about combining performance and readability by making a "Key" class
that contains static final int fields. Something like this:

class HashKeys
{
HashKeys ( ) { }

protected static final int X_FACTOR_RULE = 1;
protected static final int Y_FACTOR_RULE = 2;
}

Then, for example, I can just use HashKeys.X_FACTOR_RULE as a key value.

Alternatively, I can take the Interger approach.

class HashKeys
{
HashKeys( )
{
this.X_FACTOR_RULE = new Integer(1);
this.Y_FACTOR_RULE = new Integer(2);
}
protected Integer X_FACTOR_RULE;
protected Integer Y_FACTOR_RULE;
}

In that case, I'd make HashKeys a Singleton.

Note that I use protected since the Hash Table will be used only by
classes in the same package as itself. Do either of these approaches
seem reasonable? One better than the other? Other suggestions? I'm
inclined to go with the first approach because it seems less complex,
but I don't know if there's any catch I'm missing.

Thanks again for any recommendations!

Ken



 
 
Praveen





PostPosted: 2004-7-2 20:59:00 Top

java-programmer >> What to use as HashMap Key? What is a immutable object.?

L膩驶ie Techie wrote:
> 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).
>
> HTH,
> La'ie Techie
>

 
 
Roedy Green





PostPosted: 2004-7-2 23:30:00 Top

java-programmer >> What to use as HashMap Key? On Fri, 02 Jul 2004 12:58:41 GMT, Praveen <email***@***.com> wrote or
quoted :

>What is a immutable object.?

see http://mindprod.com/jgloss/immutable.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
Roedy Green





PostPosted: 2004-7-2 23:33:00 Top

java-programmer >> What to use as HashMap Key? On Fri, 02 Jul 2004 07:44:46 -0400, "kk_oop<no spam>" <"kk_oop<no
spam>"@yahoo.com> wrote or quoted :

>Then, for example, I can just use HashKeys.X_FACTOR_RULE as a key value.

Hashtables are for lookup by unique identifier. They are not a way of
classifying, dividing a set into subsets. So to me, your code makes
no sense.

See http://mindprod.com/jgloss/hashcode.html for tips on computing
them.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
Chris Smith





PostPosted: 2004-7-2 23:50:00 Top

java-programmer >> What to use as HashMap Key? La'ie Techie wrote:
> 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).

This is, indeed, a problem for some poorly conceived objects (including
all instances of mutable classes from the Collections API, for example).
Most mutable classes, though, correctly inherit the default
implementations of hashCode and equals from Object, and those
implementations are quite suitable for use as keys in a HashMap.

The problem is that various people have decided on their own meaning for
when one object "equals" another, and those meanings are mutually
incompatible. Ironically, the meaning defined for the objects *in* a
HashMap is different from (and incompatible with) the meaning defined
for HashMap itself!

It would be far better if objects that wanted to establish some concept
of temporary similarity or equivalence of contents would define their
own methods for doing so, instead of abusing equals for that purpose;
but we've now reached the point of having misuse of equals codified in
the API specification. It can no longer be undone.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
 
Chris Smith





PostPosted: 2004-7-2 23:53:00 Top

java-programmer >> What to use as HashMap Key? "kk_oop<no spam>" <"kk_oop<no spam>"@yahoo.com> wrote:
> How about combining performance and readability by making a "Key" class
> that contains static final int fields. Something like this:
>
> class HashKeys
> {
> HashKeys ( ) { }
>
> protected static final int X_FACTOR_RULE = 1;
> protected static final int Y_FACTOR_RULE = 2;
> }

If you know all of the possible key values at compile time, then don't
use a HashMap at all! Define a class to hold the set of values.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
 
Chris Smith





PostPosted: 2004-7-3 8:53:00 Top

java-programmer >> What to use as HashMap Key? "kk_oop<no spam>" <"kk_oop<no spam>"@yahoo.com> wrote:
> Well, I know the key values at compile time, but I don't know the values
> that will be stored with the keys. So in my example, when I called put(
> ), it would be given HashKeys.X_FACTOR_RULE as the key and some other
> object as the rule item to store. That item will not be known at
> compile time. However, it will need to be stored and accessed by a
> unique identifier (which would be the value of X_FACTOR_RULE).
>
> Does that make sense?

I'd still say no. What about:

public class MyValues
{
private Object xFactorRule = null;
private Object yFactorRule = null;
private Object zFactorRule = null;

// getter and setter methods
}

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
 
kk_oop





PostPosted: 2004-7-3 8:57:00 Top

java-programmer >> What to use as HashMap Key? This is a multi-part message in MIME format.



Chris Smith wrote:

>"kk_oop<no spam>" <"kk_oop<no spam>"@yahoo.com> wrote:
>
>
>>How about combining performance and readability by making a "Key" class
>>that contains static final int fields. Something like this:
>>
>>class HashKeys
>>{
>> HashKeys ( ) { }
>>
>> protected static final int X_FACTOR_RULE = 1;
>> protected static final int Y_FACTOR_RULE = 2;
>>}
>>
>>
>
>If you know all of the possible key values at compile time, then don't
>use a HashMap at all! Define a class to hold the set of values.
>

Well, I know the key values at compile time, but I don't know the values
that will be stored with the keys. So in my example, when I called put(
), it would be given HashKeys.X_FACTOR_RULE as the key and some other
object as the rule item to store. That item will not be known at
compile time. However, it will need to be stored and accessed by a
unique identifier (which would be the value of X_FACTOR_RULE).

Does that make sense?

I suppose an alternative would be to use these key values as indexes
into an ArrayList. Would that be a better approach?

Thanks again,

Ken



>
>
>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=ISO-8859-15">
<title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
<br>
<br>
Chris Smith wrote:<br>
<blockquote type="cite"
cite="email***@***.com">
<pre wrap="">"kk_oop<no spam>" <"kk_oop<no spam>"@yahoo.com> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">How about combining performance and readability by making a "Key" class
that contains static final int fields. Something like this:

class HashKeys
{
HashKeys ( ) { }

protected static final int X_FACTOR_RULE = 1;
protected static final int Y_FACTOR_RULE = 2;
}
</pre>
</blockquote>
<pre wrap=""><!---->
If you know all of the possible key values at compile time, then don't
use a HashMap at all! Define a class to hold the set of values.</pre>
</blockquote>
<br>
Well, I know the key values at compile time, but I don't know the
values that will be stored with the keys.?So in my example, when I
called put( ), it would be given HashKeys.X_FACTOR_RULE as the key and
some other object as the rule item to store.?That item will not be
known at compile time.?However, it will need to be stored and accessed
by a unique identifier (which would be the value of X_FACTOR_RULE).<br>
<br>
Does that make sense?<br>
<br>
I suppose an alternative would be to use these key values as indexes
into an ArrayList.?Would that be a better approach?<br>
<br>
Thanks again,<br>
<br>
Ken<br>
<br>
<br>
<br>
<blockquote type="cite"
cite="email***@***.com">
<pre wrap="">

</pre>
</blockquote>
</body>
</html>

 
 
Hemal Pandya





PostPosted: 2004-7-7 5:16:00 Top

java-programmer >> What to use as HashMap Key? =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?= <email***@***.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