小编STE*_*YAO的帖子

如何为现有密钥添加值

我正在使用循环语句从结果集中添加映射中的键和值,如果该键已存在,则更新该键并添加其值.

这是我的代码:

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))) {
      // update this key and add to its existing value
    }
    else {
      item_id.put(rs.getInt(1),rs.getInt(2));
    }
  }
Run Code Online (Sandbox Code Playgroud)

java hashmap

5
推荐指数
1
解决办法
400
查看次数

标签 统计

hashmap ×1

java ×1