STEPHEN YAO
STEPHEN YAO

Reputation: 105

How to add value to an existing key

I am using a loop statement to add keys and value in the map from a result set and if the key already exists, update that key and add to its value.

This is my code:

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));
    }
  }

Upvotes: 5

Views: 1082

Answers (5)

Zbynek Vyskovsky - kvr000
Zbynek Vyskovsky - kvr000

Reputation: 18825

In java 8 you can make the full if else just a single statement:

item_id.compute (rs.getInt (1), (key, old) -> (old == null ? 0 : old)+rs.getInt (2));

Upvotes: 4

Ali Dehghani
Ali Dehghani

Reputation: 48133

In Java 8, you can use merge:

Integer key = rs.getInt(1);
Integer value = rs.getInt(2);
item_id.merge(key, value, Integer::sum);

Also, make sure that key and value are Not Null

If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.

Upvotes: 0

Abdelhak
Abdelhak

Reputation: 8387

Try to use this:

  while(rs.next()){
            if(item_id.containskey(rs.getInt(1))){
             Integer value = item_id.get(rs.getInt(1));
             if(value == null) 
               value = 0;
              item_id.put(rs.getInt(1), value);
                      // update this key and add to its existing value
            }
  item_id.put(id, previousValue + x);

Upvotes: 1

James Wierzba
James Wierzba

Reputation: 17548

Get the old value from the map, add the value to it, and put it back in 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))){
                    // update this key and add to its existing value
                    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));
                }
      }

Upvotes: 1

wwadge
wwadge

Reputation: 3544

To do that you need to make your value a collection eg: Map<Integer, List<Integer>>

or a simpler solution would be to use google guava library: Multimap<Integer, Integer>

Upvotes: 0

Related Questions