Get the old value from the map, add the value to it and return it to the map.
Map<Integer, Integer> item_id = new Hashmap<Integer , Integer>();
for(i=0 ; i<roomtype_id.length(); i++)
{
String query ="SELECT item_id , quantity from amenities_tb where roomtype_id = '"+roomtype_id.get(i)+"'"
PreparedStatement pst = connection.preparedStatement(query);
Result Set rs = pst.executeQuery();
while(rs.next()){
if(item_id.containskey(rs.getInt(1))){
int value = item_id.get(rs.getInt(1));
value += rs.getInt(2);
item_id.put(rs.getInt(1), value);
}else{
item_id.put(rs.getInt(1),rs.getInt(2));
}
}
source
share