Gakuo
Gakuo

Reputation: 855

Java adjusting nested Hashmap value

I was trying to use ideas given here to adjust a nested hashmap but my solution does not work: How to update a value, given a key in a java hashmap?. My nested hashmap is shoppingLists. It contains an outer hashmap of shopping list listID as key and a hashmap of items as values. The items hashmap contains itemName as key and the amount of the item as the value. The adjustItemAmount attempts to adjust the amount of an item by a given amount x.

HashMap<String, HashMap<String, Integer>> shoppingLists = new HashMap<>();

public void adjustItemAmount(String itemName, int x, String listID) {
  int current_amount = shoppingLists.get(listID).get(itemName);
  HashMap<String, Integer> items = shoppingLists.get(listID);
  HashMap updatedItems = items.put(itemName, items.get(itemName) + x);
  shoppingLists.put(listID, updatedItems);
}

The line HashMap updatedItems = items.put(itemName,items.get(itemName)+x); states that Java expects a hashmap but gets an integer. I do not see how that is the case.

Upvotes: 0

Views: 1950

Answers (4)

Dinesh
Dinesh

Reputation: 1086

put in HashMap returns the Value object, in this case Integer. You are trying to assign this Integer to HashMap. Remove the assignment, your code will work. Replace

public void  adjustItemAmount (String itemName, int x,String listID){
            int current_amount = shoppingLists.get(listID).get(itemName);
            HashMap <String,Integer> items = shoppingLists.get(listID);
            items.put(itemName,items.get(itemName)+x);
            shoppingLists.put(listID,items);    
        }

Upvotes: 1

curlyBraces
curlyBraces

Reputation: 1105

HashMap's put method does not return a HashMap. It returns the value. Hence this line is incorrect:

    HashMap updatedItems= items.put(itemName,items.get(itemName)+x);

The return type would be Integer because items map is of <String, Integer> type.

Upvotes: 3

Rishikesh Dhokare
Rishikesh Dhokare

Reputation: 3589

Refer the documentation for put -

Returns : the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#put(K,%20V)

So, this

HashMap updatedItems= items.put(itemName,items.get(itemName)+x);

will fail the compilation and can not return a HashMap but it returns an Integer

Instead, it should be -

Integer updatedItems= items.put(itemName,items.get(itemName)+x);

Upvotes: 2

dunni
dunni

Reputation: 44515

You write

HashMap updatedItems= items.put(itemName,items.get(itemName)+x);

However the put method returns the previous value of the key, which was updated, or null if there was no value, not the HashMap. See https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#put-K-V-

Change the line to

Integer addedValue = items.put(itemName,items.get(itemName)+x);

or just

items.put(itemName,items.get(itemName)+x);

Upvotes: 2

Related Questions