In Java, you update a value in a HashMap by calling put() with an existing key — it overwrites the old value:
HashMap<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 87);
scores.put("Alice", 99); // update: Alice is now 99
scores.put("Charlie", 72); // add new entry
put(key, value) is used for both inserting and updating. You can also use replace() to only update existing keys:
scores.replace("Alice", 99); // only updates if key exists
Assignment
Follow the Coding Tutorial and update Alice's score to 99!
Hint
Use scores.put("Alice", 99).
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
