2016-10-12 9 views
1

keyset()の値をhashmapにして、それらをExcelシートに印刷しようとしています。そして、これは私のhashmapがどのように見えるかされていますdtoクラスをキーの値として含むハッシュマップをどのように反復処理できますか?

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

したがって、上記の中で、CodaReportDTOは、特定の日付の要素が含まれています。

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

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

ので(つまり、日付の値)キーの値を反復して取得するためには、それは次のようになります。だから私は、最初の日付を印刷するために、このようなhashmap keysetを反復しようとしました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())); 
     } 

EDIT

DTOクラスがhere可能です。

どこが間違っていますか?どんな助けでも感謝します。私はあなたの質問の権利を得た場合

+1

'CodaReportDTO'が' '一覧<地図<文字列、文字列を>>実装していなければなりませんか?または、そのデータ型のメンバを持っていますか? – 4castle

+1

PlzあなたのCodaReportDTOクラスを表示 – mhasan

+0

@ 4castle nopは 'List'を実装していません。 – Kulasangar

答えて

1

は、あなたは、このことができます。この

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 
    ... 
} 

希望のようなものを必要とします!

幸運を祈る!

+0

ありがとう、人生保護人:) – Kulasangar

1

あなたは以下にキャストに問題があるが、コード

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

を言及したことは、理想的には

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

ありがとう:)はい私は何とかそれをキャストしましたが、私が持っていた問題は繰り返しでした。 – Kulasangar

関連する問題