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.

3
  • So, what's the problem with this code? Commented Aug 9, 2018 at 4:22
  • Edited the question to indicate the problem. Commented Aug 9, 2018 at 4: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. At the moment we can't execute your code without making assumptions about your data. Commented Aug 9, 2018 at 4:26

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

1 Comment

so from what I tested so far, I dont think we need the last line as outer map is updated when items are updated.?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.