HasMap插入空值而不是string?

下面是我正在尝试使用Apache POI读取Excel数据的代码,并希望通过将第一列值作为键并将最后一列作为值来将第一列和最后一列的值存储在Hashmap中。 在将值放入散列表中时,我正面临一个问题。 我的预期产出应该是这样的

Key value UserDefined Xpath_original seldriver xpath;//div[@id='mBody'] Selenium xpath;//table[@id='choice']/tbody/tr/td[1]/ul/li[1] 

但我得到的输出为

*

 null null null null xpath;//div[@id='mBody'] 

*以下是我的一段代码

 package com.selenium.common; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ReadMacroExcel { public static void main(String[] args) { try { File f=new File("C:\\Users\\Rajaram.8.Kannuri\\Downloads\\Fire-IEBrowser1.4.xlsm"); HashMap <String,String> innerMap =null; Workbook workbook = WorkbookFactory.create(f); System.out.println(workbook); int numberOfSheets = workbook.getNumberOfSheets(); org.apache.poi.ss.usermodel.Sheet sheet=null; String key = ""; String value = ""; //Get the sheet in the xlsx file for (int i = 0; i < numberOfSheets; i++) { sheet = workbook.getSheetAt(i); System.out.println(sheet.getSheetName()); int firstColumn = sheet.getRow(0).getFirstCellNum(); int lastColumn = sheet.getRow(0).getLastCellNum(); Iterator<Row> rowIterator = sheet.iterator(); innerMap = new HashMap<String,String>(); while(rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); if(cell.getColumnIndex()==firstColumn||cell.getColumnIndex()==lastColumn-1) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: key = cell.getStringCellValue(); /*System.out.print(key + "\t");*/ /* Thread.sleep(5000);*/ break; case Cell.CELL_TYPE_FORMULA: value = cell.getRichStringCellValue().toString(); /*System.out.print(value + "\t");*/ /*Thread.sleep(5000);*/ break; } System.out.print(innerMap.put(key,value)); } } System.out.println(""); } /* System.out.println(innerMap.get("UserDefined"));*/ } } catch (Exception e) { e.printStackTrace(); } } } 

任何人都可以帮助我解决这个问题?

看来你正试图打印插入的值

 System.out.print(innerMap.put(key,value)); 

但是, put返回与给定键相关联的先前值 ,如果没有该键的映射, put返回null

另见https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#put-KV-

你应该使用类似的东西

 innerMap.put(key,value); // add the new mapping to the hashmap System.out.print(innerMap); // dumps the complete map with all entries System.out.print(innerMap.get(key)); // dumps the value for the recently inserted key