我如何迭代包含dto类的hashmap作为key的值?

我试图获得hashmapkeyset()的值,并将其打印到Excel表格中。 这就是我的hashmap样子:

 public Map<String, CodaReportDTO> dateAndDTO = new HashMap<>(); //hashmap for date and the dto 

所以在上面, CodaReportDTO包含特定date的元素。 所以我尝试像这样迭代hashmap keyset ,以便首先打印date:

  for (String dateKey : dateAndDTO.keySet()) { //dateAndDTO is the object Row tableDataRow = sheet.createRow(tableDataRowCount); Cell cell = tableDataRow.createCell(1); cell.setCellValue(dateKey); } 

因此,为了迭代和获取键的值(换句话说,date的值),它应该是一个List或一个Map 。 我该如何做一个包含DTO类的hashmap

我必须做这样的事情,但不能把类放到List

 List<Map<String, String>> tableCellData = (List<Map<String, String>>) dateAndDTO.get(dateKey); for (Map<String, String> singleCellTableData : tableCellData) { int dateCellRef = dateCellReferences.get(singleCellTableData.keySet().iterator().next()); Cell tableCell = tableDataRow.createCell(dateCellRef); tableCell.setCellValue(Integer.parseInt(singleCellTableData.values().iterator().next())); } 

编辑

DTO类在这里可用。

我哪里错了? 任何帮助,可以赞赏。

如果我的问题得到解决,你需要像这样的东西

 public Map<String, CodaReportDTO> dateAndDTO = new HashMap<>(); //hashmap for date and the dto Set<Entry<String, CodaReportDTO>> entrySet = dateAndDTO.entrySet(); for(Entry<String, CodaReportDTO> entry : entrySet){ entry.getKey(); //your String key ie date in your case entry.getValue(); //your DTO value for this key ... } 

希望这可以帮助!

祝你好运!

您在使用下面提到的代码时遇到问题

 List<Map<String, String>> tableCellData = (List<Map<String, String>>) dateAndDTO.get(dateKey); 

它应该是理想的

 CodaReportDTO tableCellData = (CodaReportDTO) dateAndDTO.get(dateKey);