aspose file tools*
The moose likes Beginning Java and the fly likes Can an array be used as a HashMap Key? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can an array be used as a HashMap Key?" Watch "Can an array be used as a HashMap Key?" New topic
Author

Can an array be used as a HashMap Key?

Matt Wilcko
Ranch Hand

Joined: Sep 23, 2003
Posts: 65
Can an array be used as a HashMap Key? My concern is how HashMap compares if two keys are indentical.. since using '=' will always return false, is there something I can override or is there an easier way to do this?

Also, when I call .get() on the HashMap supplying an array as the argument, I get a ClassCastException?
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
I'm confused by your post - you say the using "=" will always result in false.... I'll assume you meant "==", but i still don't buy it. Do you mean:

would print "false"? I get "true".

Regarding the ClassCastException when you try to use an array with HashMap.get() - I didn't get that, either:



My output is:

true
MyString

which seem to imply that an array is a perfectly valid hash key.

One thing to point out: Since arrays extend object, but don't override hashCode() or equals(), you get the default implementations defined by Object. These require that the array is actually the exact same one as is being compared to - so it can't just be "equivalent". That is, another array of the same type, with the same elements in the same order, won't work. Is that what you were trying to do? How did the ClassCastException come about? Can you post some code?

-- Jon
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410

Do you mean:

code:


int[] a = new int[5];
int[] b = a;
System.out.println(a == b);


would print "false"? I get "true".

I think he means that two arrays with the same content will return false when compared with == (because they are different references). i.e:

Will print 'false'.
Matt Wilcko
Ranch Hand

Joined: Sep 23, 2003
Posts: 65
"I think he means that two arrays with the same content will return false when compared with == (because they are different references)."

Yes.. accidentally typed = instead of == in my post. What I am asking about is since when the HashMap compares keys, it effectively uses the == instead of Arrays.equals(a,b), so the arrays will never be equal even if they have identical content.

For example:

import java.util.HashMap;

public class DoIt2 {
public static void main(String [] args) {
HashMap hm = new HashMap();

String[] a = {"bob", "mary"};
String[] b = {"bob", "mary"};
hm.put(a, "MyString");

System.out.println(hm.get(b));
}

}

This will return null.

Is there any way work around this?
[ April 24, 2005: Message edited by: Matt Wilcko ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Matt Wilcko:
Is there any way work around this?


Use a List instead of an array.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Or use a TreeMap instead of a HashMap, and supply your own Comparator object.


[Jess in Action][AskingGoodQuestions]
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
Or use an IdentityHashMap.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stuart Gray:
Or use an IdentityHashMap.


That wouldn't differ in behaviour at all in this case, would it?
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
Er....um....no it wouldn't. Doh.
Matt Wilcko
Ranch Hand

Joined: Sep 23, 2003
Posts: 65
After experimenting with this, it appears that HashMap's do not like ArrayLists as a key. In small examples, it seems to work. But on larger ones, I eventually got a ClassCastException. Simply switching to a String key solves the problem. Just combined the two array elements with a seperator into a String.
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Matt,

I'd really like to try to understand why you get the ClassCastException when using an ArrayList or an array as a HashMap key.

Could you re-introduce the problem (temporarily) and post the stack trace for the ClassCastException?

-- Jon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can an array be used as a HashMap Key?