3

I have a HashMap which I want to convert into a array. Which the code below, I get following: [[2, 11, 10, 9], [value1, value2, value3, value4], [null, null, null, null], [null, null, null, null]] The "null" entries give me a null pointer exception. Why is it two times as big as it should be? I just want the real entries: [[2, 11, 10, 9], [value1, value2, value3, value4]]. What did I wrong?

String[][] test = getArrayFromHash(hashmap);

    public static String[][] getArrayFromHash(HashMap<String, String> hashMap){
        String[][] str = null;
        {
            Object[] keys = hashMap.keySet().toArray();
            Object[] values = hashMap.values().toArray();
            str = new String[keys.length][values.length];
            for(int i=0;i<keys.length;i++) {
                str[0][i] = (String)keys[i];
                str[1][i] = (String)values[i];
            }
        }
        return str;
    }

Thanks!

user1390816
  • 623
  • 2
  • 13
  • 34

3 Answers3

4

Refer this link:::

    String[][] arr = new String[hashmap.size()][2];
    Set entries = hashmap.entrySet();
    Iterator entriesIterator = entries.iterator();

    int i = 0;
    while(entriesIterator.hasNext()){

        Map.Entry mapping = (Map.Entry) entriesIterator.next();

        arr[i][0] = mapping.getKey().toString();
        arr[i][1] = mapping.getValue().toString();

        i++;
    }

UPdated:::: to get two 1D arrays

String[] arr1 = new String[hashmap.size()];
String[] arr2 = new String[hashmap.size()];
        Set entries = hashmap.entrySet();
        Iterator entriesIterator = entries.iterator();

        int i = 0;
        while(entriesIterator.hasNext()){

            Map.Entry mapping = (Map.Entry) entriesIterator.next();

            arr1[i] = mapping.getKey().toString();
            arr2[i] = mapping.getValue().toString();

            i++;
        }
Community
  • 1
  • 1
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
3

Allocate array of proper size like

str = new String[2][values.length];

Moreover you should assert that values and keys have the same length. And also that matching entries appear at the same position. Something like

String[] keys = hashMap.keySet().toArray(new String[0]);
String[] values = new String[keys.length];
String[][] str = {keys, values};
for(int i=0;i<keys.length;i++) values[i] = hashMap.get(keys[i]);
return str;

should do (not tested).

maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • Short, concise, and it looks like it will work to me. And just in case you didn't see my prev comment, I deleted my answer. I realized that Sets don't maintain order, and wasn't sure if I could cast to LinkedHashSet and maintain order before the cast. I see you address that here by using the keys in the keys array to get the actual values from the map itself. Very clever. +1 – jamesmortensen May 26 '12 at 09:41
  • Thx. Using an `EntrySet` may be faster. It has the overhead of creating `Map.Entry`, but eliminates the lookups. – maaartinus May 27 '12 at 11:19
3

I'm not sure if keyset and values will necessary return the values in the same order (it's not specified in the implementation doc so I'd say it's not guaranteed).

try working with entrysets:

Set<Map.Entry<String, String>> set = hm.entrySet();
int i = 0;
for (Map.Entry<String,String> me : set) {
  arr[i][0] = me.getKey();
  arr[i][1] = me.getValue()};
  i++;
}
Grims
  • 777
  • 5
  • 6