Java - 如何将地图列表添加到2D对象数组?

时间:2016-03-15 19:41:17

标签: java arrays object hashmap

我正在构建以下代码来读取excel文件并将哈希映射列表添加到2维对象数组中:

public static Object[][] getTableAsMapObject(String xlFileName, String xlSheetName) throws Exception {
    Map<String, String> dataMap = null;
    ArrayList<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
    Object[][] tabArray = null;

    String masterDataFilePath = "./data/MasterData.xlsx";
    try {
        FileInputStream ExcelFile = new FileInputStream("./data/" + xlFileName + ".xlsx");
        XSSFWorkbook excelWBook = new XSSFWorkbook(ExcelFile);
        XSSFSheet excelWSheet = excelWBook.getSheet(xlSheetName);
        row = excelWSheet.getRow(0);
        int totalCols = row.getLastCellNum();
        totalCols--;
        int startRow = 1;
        int startCol = 1;
        int ci, cj;
        int totalRows = excelWSheet.getLastRowNum();
        int activeRows = 0;
        ci = 0;
        for (int i = startRow; i <= totalRows; i++, ci++) {
            if (getCellData(excelWSheet, i, 0).trim().toUpperCase().equals("YES")
                    || getCellData(excelWSheet, i, 0).trim().toUpperCase().equals("Y")) {
                activeRows++;
            }
        }

        tabArray = new Object[activeRows][0];
        ci = 0;
        for (int i = startRow; i <= totalRows; i++) {// , ci++) {
            cj = 0;
            if (getCellData(excelWSheet, i, 0).trim().toUpperCase().equals("YES")
                    || getCellData(excelWSheet, i, 0).trim().toUpperCase().equals("Y")) {
                dataMap = new HashMap<String, String>();
                for (int j = startCol; j <= totalCols; j++) {

                    String colName = getCellData(excelWSheet, 0, j);
                    if (colName.contains("_")) {
                        String[] bits = colName.split("_");
                        String lastOne = bits[bits.length-1];
                        if (lastOne.equalsIgnoreCase("key")) {
                            dataMap = getMasterDataSet(masterDataFilePath, bits[0], getCellData(excelWSheet, i, j), dataMap);
                        }
                    } else { dataMap.put(colName, getCellData(excelWSheet, i, j)); }
                    cj++;
                }
                listOfMaps.add(dataMap);
                **tabArray = new Object[][] { {dataMap} }; //<== Here I want all the maps in listOfMaps to be added to tabArray**
                dataMap = null;
                ci++;
            }
        }
        excelWBook.close();
    } catch (FileNotFoundException e) {
        System.out.println("Could not read the Excel sheet");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Could not read the Excel sheet");
        e.printStackTrace();
    }
    return (tabArray);
}

我无法将整个地图列表放到对象数组中,而是只能获得一个地图。最终这应该是这样的:

tabArray = new Object[][] { {dataMap1}, {dataMap2}, ... };

2 个答案:

答案 0 :(得分:0)

只需分解您的map并将其输入Object[][]

即可
Map<String, String> map = new HashMap<>();
Object[][] tabArray = new Object[2][];
tabArray[0] = map.keySet().toArray();
tabArray[1] = map.values().toArray();

来自JavaDoc的keySet()values()

的注释
  

没有执行同步,所以有一点点机会   对此方法的多次调用都不会返回相同的集合。

答案 1 :(得分:0)

以下是修改后的内容:

[^()]