0

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.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Gakuo
  • 845
  • 6
  • 26
  • So, what's the problem with this code? – default locale Aug 09 '18 at 04:22
  • Edited the question to indicate the problem. – Gakuo Aug 09 '18 at 04:25
  • What do you mean by "states that Java expects a hashmap but gets an integer."? If you're getting an exception, post the complete details: exception type, message and stack trace. Also, please, create a [reproducible example](https://stackoverflow.com/help/mcve). At the moment we can't execute your code without making assumptions about your data. – default locale Aug 09 '18 at 04:26

4 Answers4

3

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.

curlyBraces
  • 1,095
  • 8
  • 12
2

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);
dunni
  • 43,386
  • 10
  • 104
  • 99
2

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

Rishikesh Dhokare
  • 3,559
  • 23
  • 34
1

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);    
        }
Dinesh
  • 1,046
  • 1
  • 8
  • 17