I have a List<Map<String, Object>> that returns output as below.
[{ID:55, Item=6455, Quantity=3, Cost=150$},{ID:89, Item=0566, Quantity=2, Cost=30$},{ID:112, Item=5477, Quantity=1, Cost=50$},{ID:345, Item=6768, Quantity=10, Cost=280$}]
I'm trying to add key value pair "Size=large" at beginning of each Map<String, Object> of List. I used below code to add the key and value.
List<Map<String, Object> returnList = null;
returnList = jdbcTemplate.query(itemQuery, extractor);
Map<String,Object> map = new LinkedHashMap<>();
map.put("Size","large");
returnList.add(map);
System.out.println(returnList);
Right now I'm getting output as:
[{ID:55, Item=6455, Quantity=3, Cost=150$},{ID:89, Item=0566, Quantity=2, Cost=30$},{ID:112, Item=5477, Quantity=1, Cost=50$},{ID:345, Item=6768, Quantity=10, Cost=280$}, {Size=large}]
How can I get below output to add value for each Map of the list?
[{Size=large, ID:55, Item=6455, Quantity=3, Cost=150$},{Size=large, ID:89, Item=0566, Quantity=2, Cost=30$},{Size=large, ID:112, Item=5477, Quantity=1, Cost=50$},{Size=large, ID:345, Item=6768, Quantity=10, Cost=280$}]
// I was able to get answer for my question based on #Quadslab reply as below//
int size=returnList.size();
for(Map<String,Object> map : returnList) {
Map<String,Object> mapcopy = new HashMap<String, Object>();
for(Map<String,Object> entry: map.entrySet()){
mapcopy.put(entry.grtKey(),entry.getValue());
}
map.clear();
map.put("Size","large");
map.putAll(mapcopy);
}