tom
tom

Reputation: 5504

How to add HashMap values to List and print the same

I am trying to add map values to list for my scenario as below.

I have select statement which returns n-no of columns and row, I am storing them into List of Hash Map of type String and pass it to some other method to produce an EXCEL file out of the result.

i am unable to see any data in the list

Please advice where i am going wrong.

while (result.next()) { 
  resultValues.put("PARTC_ID",result.getString("PARTC_ID"));  
  resultValues.put("FILE_NME",result.getString("FILE_NME"));  
  resultValues.put("LOC_ID",result.getString("LOC_ID")); 
  resultValues.put("CRTE_DTE",result.getString("CRTE_DTE")); 
  resultValues.put("CRTE_BY",result.getString("CRTE_BY")); 
  value.add(resultValues); resultValues.clear(); System.out.println(value); 
} 

Upvotes: 0

Views: 185

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201409

You're clearing your Map after adding it to the List. The Map references are all thus the same (and empty)... I think you want to make this change -

// resultValues.clear(); // No, if you need another Map... do this
resultValues = new HashMap<String, String>();

Then to iterate your value List try

for (HashMap<String, String> map : value) {
  for (String key : map.keySet()) {
    System.out.printf("key[%s] = %s\n", key, map.get(key));
  }
}

Upvotes: 1

Related Questions