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!

0

3 Answers 3

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++;
        }
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot! How can I make Strings from these Objekts? I want to put all values into a Spinner, therefore I need Strings.
I get this exception:05-26 14:02:27.473: W/System.err(22784): java.lang.ClassCastException: [[Ljava.lang.Object;
can you post your complete logcat?
mapping.getKey().toString(); replace with mapping.getKey(); and try
The Error was on the conversion from the 2D-array to a 1D array. This was the code: for(int i = 0, k = 0; i < test.length; i++) for(int j = 0; j < test[0].length; j++, k++) colors[k] = test[i][j];
|
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).

2 Comments

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
Thx. Using an EntrySet may be faster. It has the overhead of creating Map.Entry, but eliminates the lookups.
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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.