1. 程式人生 > >HashMap轉為二維陣列,Map遍歷

HashMap轉為二維陣列,Map遍歷

一、HashMap轉為二維陣列

private Object[][] hashMapToTwoDimensionalArray(Map sourceHashMap) {
        Object[][] result = new Object[sourceHashMap.size()][2];
        Object[] keys = sourceHashMap.keySet().toArray();
        Object[] values = sourceHashMap.values().toArray();
        for (int i = 0; i < keys.length; i++) {
            result[i][0] = keys[i];
            result[i][1] = values[i];
        }
        return result;
    }

 二、遍歷Map 

 for (String key: map.keySet()) {
      String value= map.get(key);
 }