gpt4 book ai didi

java - 如何迭代包含 dto 类作为键值的 HashMap ?

转载 作者:行者123 更新时间:2023-11-30 02:46:52 25 4
gpt4 key购买 nike

我正在尝试获取 hashmapkeyset() 值并将其打印到 Excel 工作表中。这就是我的 hashmap 的样子:

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

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

    for (String dateKey : dateAndDTO.keySet()) { //dateAndDTO is the object

Row tableDataRow = sheet.createRow(tableDataRowCount);
Cell cell = tableDataRow.createCell(1);
cell.setCellValue(dateKey);
}

因此,为了迭代并获取键的值(换句话说,日期的值),它应该是 ListMap 。如何对包含 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 类可用 here .

我哪里出错了?如有任何帮助,我们将不胜感激。

最佳答案

如果我答对了你的问题,你需要这样的东西

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 i.e. date in your case
entry.getValue(); //your DTO value for this key
...
}

希望这有帮助!

祝你好运!

关于java - 如何迭代包含 dto 类作为键值的 HashMap ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39991094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com