Java HashMap Convert hashMapToArray(HashMap map)

Here you can find the source of hashMapToArray(HashMap map)

Description

Converts a HashMap to a 2-column array {key, value}

License

Open Source License

Parameter

Parameter Description
map a parameter

Declaration

public static Object[][] hashMapToArray(HashMap<String, Object> map) 

Method Source Code

//package com.java2s;
/*//from w  ww .  j av a2s . c o m
 * 
This file is part of YaBS.
    
YaBS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
YaBS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
    
 * 
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Converts a HashMap to a 2-column array {key, value}
     * @param map
     * @return
     */
    public static Object[][] hashMapToArray(HashMap<String, Object> map) {
        return mapToArray(map);
    }

    /**
    * Converts a HashMap to a 2-column array {key, value}
    * @param map
    * @return
    */
    public static Object[][] mapToArray(Map<String, Object> map) {
        Object[][] data = new Object[map.size()][2];
        String[] arr = map.keySet().toArray(new String[] {});

        for (int idx = 0; idx < arr.length; idx++) {
            data[idx][0] = arr[idx];
            data[idx][1] = map.get(arr[idx]);
        }

        return data;
    }
}

Related

  1. HashMapToParamsString(HashMap params)
  2. hashMapToString(HashMap map)
  3. toArrayListHashMapStrObj(Object object)