File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can an array be used as a HashMap Key?

 
Matt Wilcko
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 410
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matt Wilcko:
Is there any way work around this?


Use a List instead of an array.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use a TreeMap instead of a HashMap, and supply your own Comparator object.
 
Stuart Gray
Ranch Hand
Posts: 410
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use an IdentityHashMap.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 410
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er....um....no it wouldn't. Doh.
 
Matt Wilcko
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic