Arrays in a Map

Sometimes there are programming issues which let me think: I’m a java-newbie!

E.g. the fact, that arrays can’t be used as key in a map. But arrays as value can be used without  a problem.

JUnit-test example for keys (won’t work):

String[] key = {"1","2","3"};
Map map =  new HashMap();
map.put(key, "a");

String[] newKey = {"1","2","3"};
assertEquals(map.get(newKey), "a");

JUnit-test example for values:

String[] val = {"1","2","3"};
Map map = new HashMap();
map.put("1", val);
assertEquals(val, map.get("1"));

Is there anyone who can explain that?
It would be logical for me, if array couldn’t be used neither as key nor at value. An array isn’t really a single object! But anyway …

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: