[英]Java - How to add list of maps to 2D object array?
I am building the below code to read an excel file and get a list of hashmaps into a 2 dimensional object array: 我正在构建下面的代码来读取一个Excel文件,并将哈希表列表转换为二维对象数组:
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);
}
I am unable to get the whole list of maps into object array, instead I am able to get only one map. 我无法将整个地图列表放入对象数组,而是只能获得一张地图。 Eventually this should do something like: 最终,这应该执行以下操作:
tabArray = new Object[][] { {dataMap1}, {dataMap2}, ... };
Just decompose your map
and enter it in your Object[][]
只需分解map
然后将其输入到Object[][]
Map<String, String> map = new HashMap<>();
Object[][] tabArray = new Object[2][];
tabArray[0] = map.keySet().toArray();
tabArray[1] = map.values().toArray();
keySet()
and values()
JavaDoc中关于keySet()
和values()
No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same set. 由于没有执行任何同步,因此,对该方法的多次调用将不会全部返回相同的集合的可能性很小。
Below is the modified which worked for me: 以下是对我有用的修改:
public static Object[][] getTableAsMapObject(String xlFileName, String xlSheetName) throws Exception {
Map<String, String> dataMap = null;
Object[][] tabArray = null;
String masterDataFilePath = "./data/MasterData.xlsx";
try {
FileInputStream ExcelFile = new FileInputStream("./data/" + xlFileName + ".xlsx");
// Access the required test data sheet
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;
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][1]; //***<<< Changing 0 to 1 did the trick
ci = 0;
for (int i = startRow; i <= totalRows; i++) {
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)); }
}
tabArray[ci][0] = dataMap;
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.