我尝试从Excel迭代数据。首先,我将一个键中的值放在HashMap中。
这是我的代码段:
public class HashMapEx {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("/path-to-excel");
XSSFWorkbook wb = new XSSFWorkbook(fis);
wb.close();
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> ri = sheet.rowIterator();
int rowCount = sheet.getLastRowNum();
Row re = ri.next();
Map<String, List<String>> hs = new HashMap<String, List<String>>();
ArrayList<String> al = new ArrayList<String>();
if (re.getCell(0).getStringCellValue().equalsIgnoreCase("TestCases")) {
for (int i = 0; i < rowCount; i++) {
Row ro = ri.next();
Iterator<Cell> cell = ro.cellIterator();
if (cell.hasNext()) {
Cell ci = cell.next();
al.add(ci.getStringCellValue());
}
}
}
hs.put(re.getCell(0).getStringCellValue(), new ArrayList(Arrays.asList(al)));
for (Map.Entry<String, List<String>> entry : hs.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
}
输出:
Key = TestCases, Value = [[Login, Creation, Submission]]
现在,我想通过将键用作“ TestCases”来从哈希映射中一次检索值。这是我正在尝试的:
if(hs.containsKey("TestCases")) {
for(int i=0;i<hs.values().size();i++)
System.out.println("value: " + hs.get(i));
}
如果对此有任何建议,请告诉我?预先感谢!