5

I have a hashmap like this:

HashMap<String, String[]> map = new HashMap<String, String[]>();

I want to convert this to an array, say Temp[], containing as first value the key from the hashmap and as second the String[]-array from the map, also as array. Is this possible? How?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
tetsuya
  • 51
  • 1
  • 1
  • 2
  • 2
    Your question isn't clear - are you talking about an array of a new class, where each instance contains the key and value from the map? – Jon Skeet Mar 16 '11 at 07:24

4 Answers4

5

See same question here:

hashMap.keySet().toArray(); // returns an array of keys
hashMap.values().toArray(); // returns an array of values
Community
  • 1
  • 1
Benoit Thiery
  • 6,325
  • 4
  • 22
  • 28
  • code is like this by now: ` HashMap map = new HashMap(); map.put("key1", new String[]{"value1", "test1"}); map.put("key2", new String[]{"value2"}); Object[][] twoDarray = new String[map.size()][]; Object[] keys = map.keySet().toArray(); Object[] values = map.values().toArray(); System.out.println(values[1]); System.out.println(values[2]);` output is broken. gives back [Ljava.lang.String;@1f33675 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at test.main(test.java:17) – tetsuya Mar 16 '11 at 07:35
  • @tetsuya You are trying to access the third element backed in the Values() HashSet, which according to your input **doesnt exist**, hence u get that RuntimeException – Narayan Mar 16 '11 at 07:40
  • @tetsuya Index in arrays are starting at 0. So you can access only values[0] and values[1] if you have 2 entries in your map. – Benoit Thiery Mar 16 '11 at 07:42
1

Hm. Your question is really strange, but here is what you asked:

HashMap<String, String[]> map = new HashMap<String, String[]>();
String[] keys = map.keySet().toArray();
Object[] result = new Object[keys.length*2]; //the array that should hold all the values as requested
for(int i = 0; i < keys.length; i++) {
  result[i] = keys[i]; //put the next key into the array
  result[i+1] = map.get(keys[i]); //put the next String[] in the array, according to the key.
}

But man, for what should you ever need something like this? Whatever you want to do, The chance is over 99% that you don't need to write something like this...

erikbstack
  • 12,878
  • 21
  • 81
  • 115
0

I guess, you just need to write a method, that does what you want.

weekens
  • 8,064
  • 6
  • 45
  • 62
0

I'm not sure if this is what you needed but here is your code that I modified:

    Map<String, String[]> map = new HashMap<String, String[]>();
    map.put("key1", new String[]{"value1", "test1"});
    map.put("key2", new String[]{"value2"});

    Object[] keys = map.keySet().toArray();
    Object[] values = map.values().toArray();

    System.out.println("key = " + keys[0] + ", value #1 = " + ((Object[])values[0])[0]);
    System.out.println("key = " + keys[1] + ", value #1 = " + ((Object[])values[1])[0] + ", value #2 = " + ((Object[])values[1])[1]);

Output:

key = key2, value #1 = value2
key = key1, value #1 = value1, value #2 = test1

Note that under key[0] you have "key2" instead of "key1". That is because HashMap doesn't keep the keys in the same order you add them. To change that you must choose another Map implementation (for example TreeMap if you want to have alphabetical order).

How this works? Java allows you to cast arrays to Object. toArray() method returns Object[] - an array of Objects, but those objects are also arrays (of String - as you defined in your map)! If you print just value[0] it would be like printing mySampleArray where mySampleArray is:

Object[] mySampleArray = new Object[5];

So you call it on the whole array not an element. This is why you get [Ljava.lang.String;@7c6768 etc. (which is className@HashCode - thats what default toString() method does).

In order to get to your element you have to go deeper.

((Object[])values[0])[0]

This means: Take values[0] (we know it holds an array), cast it to array and take first element.

I hope this helps, please let me know if you need any additional info.

mckulpa
  • 374
  • 4
  • 9