Problem overview
A shop keeps its stock in inventory, a HashMap<String, Integer> from item names to counts. The method restock receives an item that is already a key and an amount of at least 1. It adds amount to that item's count and returns the updated map. Every other entry stays as it was.
With {"apples": 5, "bananas": 2, "cherries": 10}, item = "bananas" and amount = 3, the bananas go from 2 to 5, and the apples and cherries keep their counts. With {"milk": 0} and an amount of 12, the result is {"milk": 12}.
Approach: read the old count, write back the new one
A map has no "add to this value" operation. The update takes two moves: read the current count with get, add amount, and store the sum under the same key with put. When put receives a key that is already in the map, it replaces that key's value and leaves the other entries alone.
Step by step
- Put the item back with its new count. The value is
inventory.get(item) + amount, the old count plus the delivery. - Return
inventory. The map was changed in place, so it already holds every entry with only one count different.
The solution in Java
class Solution {
public HashMap<String, Integer> restock(HashMap<String, Integer> inventory, String item, int amount) {
inventory.put(item, inventory.get(item) + amount);
return inventory;
}
}
Java details behind the two lines:
- Boxing. The map stores
Integerobjects, butinventory.get(item) + amountis ordinaryintarithmetic. Java unboxes the stored value, adds, and boxes the sum again whenputstores it. getcan returnnull. For a missing key, unboxing thatnullthrows aNullPointerException. For example,restockon an empty map with the item"tea"fails that way. The lesson promises the item is a key, so the plaingetis safe here.getOrDefault(item, 0)would also handle new items.- The map is changed in place. The method returns the same object it was given, not a copy.
- Order doesn't matter. A
HashMapdoesn't promise any entry order, which is why the lesson ignores it. Printing aHashMapcan list the entries in a surprising order.
Other ways to write the update
The two-step get and put is the clearest form. Java's maps also offer merge, which does the read, the addition and the write in one call: inventory.merge(item, amount, Integer::sum) turns {"bananas": 2} into {bananas=5} for an amount of 3. When the key is absent, merge simply stores the amount, so merge("kiwi", 4, Integer::sum) on that map adds kiwi=4.
The version with getOrDefault handles new items too: inventory.put(item, inventory.getOrDefault(item, 0) + amount) adds ink=5 to a map that has no ink. This lesson only needs the plain get, because the item is always present.
Edge cases and common mistakes
- Sold-out items. A count of 0 works like any other count:
0 + 12is 12. - Putting only
amount.inventory.put(item, amount)replaces the count instead of adding to it. The milk example would pass by accident, and the bananas would end at 3 instead of 5. - Forgetting the return. The map is updated, but the method must still return it.
- Touching other keys. Only
itemchanges. There's no loop over the map.
Testing
{"apples": 5, "bananas": 2, "cherries": 10},"bananas", 3 gives{apples=5, bananas=5, cherries=10}.{"milk": 0},"milk", 12 gives{milk=12}.{"pens": 7, "paper": 3},"pens", 1 gives{paper=3, pens=8}.
