Start learning free

LESSON 124 OF 671 · DICTIONARIES

Updating Dictionary Values in Java

Lesson

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

STEP 1 OF 1
    Ctrl + Enter to check