Java: how to convert HashMap<String, Object> to array

burntsugar picture burntsugar · Jul 7, 2009 · Viewed 294.5k times · Source

I need to convert a HashMap<String, Object> to an array; could anyone show me how it's done?

Answer

Landon Kuhn picture Landon Kuhn · Jul 7, 2009
hashMap.keySet().toArray(); // returns an array of keys
hashMap.values().toArray(); // returns an array of values

Edit

It should be noted that the ordering of both arrays may not be the same, See oxbow_lakes answer for a better approach for iteration when the pair key/values are needed.