How can I iterate a hashmap that contains a dto class as the value for the key?


I'm trying to get the values of the keyset() of a hashmap and print those into an excel sheet. And this is how my hashmap looks like:

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

So in the above, CodaReportDTO contains elements for a particular date. So I tried iterating the hashmap keyset like this in order to print the dates first:

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

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

So in order to iterate and get the values of a key (in other words, the values for the date), it should be either a List or a Map. How can I do this for a hashmap which contains a DTO class?

I have to do something like this but couldn't cast the class in to a 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);


The DTO class is available here.

Where am I going wrong? Any help could be appreciated.

If I got your question right , You need Something like this

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

Hope this helps!

Good luck!