Posts Tagged map

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 …

Advertisements

, ,

Leave a comment