Java-如何将地图列表添加到2D对象数组?
我正在构建以下代码以读取excel文件,并将哈希映射列表放入二维对象数组中:Java-如何将地图列表添加到2D对象数组?,java,arrays,object,hashmap,Java,Arrays,Object,Hashmap,我正在构建以下代码以读取excel文件,并将哈希映射列表放入二维对象数组中: public static Object[][] getTableAsMapObject(String xlFileName, String xlSheetName) throws Exception { Map<String, String> dataMap = null; ArrayList<Map<String, String>> listOfMaps = ne
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);
}
只需分解您的
地图
,并将其输入到您的对象[]【】
Map<String, String> map = new HashMap<>();
Object[][] tabArray = new Object[2][];
tabArray[0] = map.keySet().toArray();
tabArray[1] = map.values().toArray();
Map Map=newhashmap();
对象[][]选项卡数组=新对象[2][];
tabArray[0]=map.keySet().toArray();
tabArray[1]=map.values().toArray();
JavaDoc中关于keySet()和values()的注释
没有执行同步,因此有一点可能
对该方法的多个调用不会全部返回同一个集合
以下是对我有效的修改:
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);
}
公共静态对象[][]getTableAsMapObject(字符串xlFileName,字符串xlSheetName)引发异常{
Map dataMap=null;
对象[][]tabArray=null;
字符串masterDataFilePath=“./data/MasterData.xlsx”;
试一试{
FileInputStream ExcelFile=newfileinputstream(“./data/”+xlFileName+”.xlsx”);
//访问所需的测试数据表
XSSFWorkbook excelWBook=新的XSSFWorkbook(ExcelFile);
XSSFSheet excelWSheet=excelWBook.getSheet(xlSheetName);
row=excelWSheet.getRow(0);
int totalCols=row.getLastCellNum();
道达尔科尔斯--;
int startRow=1;
int startCol=1;
int ci;
int totalRows=excelWSheet.getLastRowNum();
int activeRows=0;
ci=0;
对于(int i=startRow;ilistofmap.toArray()
??干净的代码?单一抽象层?单一责任原则?如果您想提高代码的质量,您可能需要搜索这些术语,看看它们如何不适用于您的代码;-)您只有一个1D列表;一个地图列表。您没有列表列表,因此我不确定您希望如何访问2D对象[][]
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);
}