map.put(key, map.get(key) + 1);

should be fine. It will update the value for the existing mapping. Note that this uses auto-boxing. With the help of map.get(key) we get the value of corresponding key, then you can update with your requirement. Here I am updating to increment value by 1.

Answer from Matthew Flaschen on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java collections › java map › update the value associated with a key in a hashmap
Update the Value Associated With a Key in a HashMap | Baeldung
June 6, 2024 - Since apple is on the map, the containsKey method will return true. Therefore, the call to the put method will be executed, and the value will be updated. Since Java 8, many new methods are available that facilitate the process of updating the value of a key in the HashMap.
🌐
Blogger
javarevisited.blogspot.com › 2022 › 12 › how-to-update-value-for-key-hashmap.html
How to update value for a given key in HashMap - Java 8 getOrDefault() example
May 10, 2023 - If you call the put(key, value) method with a key which already exists in the HashMap then instead of adding a new mapping, it updates the value of existing mapping. For example, if the key "Java" is mapped to the value 1000 in a map then calling ...
🌐
Programiz
programiz.com › java-programming › examples › update-hashmap-value
Java Program to Update value of HashMap using key
Here, first, we access the value ...tln("HashMap: " + numbers); // update the value of Second // Using computeIfPresent() numbers.computeIfPresent("Second", (key, oldValue) -> oldValue * 2); System.out.println("HashMap with updated value: " + numbers); } }...
🌐
CodeGym
codegym.cc › java blog › java collections › how to update the value of an existing key in hashmap
How to update the value of an existing key in HashMap
October 11, 2023 - This can be done using the HashMap remove method. remove() not only removes the association but also returns the deleted value (if it was there before). Let's supplement our example with this operation: //easy replacement example import ...
🌐
Quora
quora.com › Is-it-possible-to-update-a-value-of-a-key-in-HashMap-in-Java
Is it possible to update a value of a key in HashMap in Java? - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
Mkyong
mkyong.com › home › java › java – how to update the value of a key in hashmap
Java – How to update the value of a key in HashMap | mkyong.com
March 2, 2021 - Map<String, Integer> map = new HashMap<>(); map.put("a", 1); System.out.println(map.get("a")); // 1 map.put("a", 2); // key `a` exists, update or replace the value System.out.println(map.get("a")); // 2 ... Map<String, Integer> map = new ...
🌐
Medium
medium.com › @AlexanderObregon › javas-map-replace-method-explained-150962a66f80
Java’s Map.replace() Method Explained | Medium
December 19, 2024 - This version of the replace() method allows you to update the value associated with a given key, but only if the key is already present in the map. If the key does not exist, the method does nothing and returns null.
🌐
GeeksforGeeks
geeksforgeeks.org › java › hashmap-replacekey-value-method-in-java-with-examples
Java HashMap replace() Method - GeeksforGeeks
July 12, 2025 - The replace() method of the HashMap class in Java is used to replace the value associated with a specific key if the key is already present in the map.
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_hashmap_replace.asp
Java HashMap replace() Method
The replace() method writes a new value to an existing entry in the map. The entry can be specified by its key, or by both its key and value.
🌐
Baeldung
baeldung.com › home › java › java collections › java map › how to modify a key in a hashmap?
How to Modify a Key in a HashMap? | Baeldung
January 8, 2024 - Now, we want to change K1 into K2 to have K2 -> V. Indeed, the most straightforward idea to achieve that is finding the entry by “K1” and replacing the key “K1” with “K2”. However, we can also remove the K1 -> V association and add a new K2 -> V entry. The Map interface offers the remove(key) method to remove an entry from the map by its key. Further, the remove() method returns the value removed from the map.
🌐
LabEx
labex.io › tutorials › java-how-to-replace-an-element-in-a-java-map-if-the-current-value-matches-the-expected-value-415492
How to replace an element in a Java Map if the current value matches the expected value | LabEx
This is useful when you need to modify or update the data stored in the Map. The most straightforward way to replace a value in a Java Map is to use the put() method.
🌐
Stack Overflow
stackoverflow.com › questions › 66302292 › changing-value-in-a-map-the-right-way
java - Changing value in a map, the right way - Stack Overflow
... FP environments usually offer "persistent" datastructures that have "modifiers" that don't change the original data but return a modified copy. For a Map, this would mean having newMap = map.updated(key, newValue).
🌐
thisPointer
thispointer.com › home › collections › java : how to update the value of an existing key in hashmap | put() vs replace()
Java : How to update the value of an existing key in HashMap | put() vs replace() - thisPointer
January 13, 2018 - It should return false because // old value of frm is not 89. assert (mapOfWords.replace("frm", 89, 42) == false); // update value of frmeee to 89 from 42. It should return false because // key 'frmeee' does not exist in map assert (mapOfWords.replace("frmeee", 89, 42) == false); } } ... {at=2, John=25, from=10, is=5} Map Contens after change : {at=2, John=25, from=67, is=5} {frm=99, at=2, John=25, from=67, is=5} {frm=99, at=2, John=25, from=56, is=5} {frm=99, at=2, John=25, from=56, is=5} {frm=888, at=2, John=25, from=56, is=5} Java : How to Remove elements from HashMap while Iterating
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java examples › java – change the value of a key in a hashmap
Java - Change the value of a key in a HashMap - Apps Developer Blog
August 16, 2022 - There are many ways to change the value of a key in HashMap. In this tutorial, you will learn the three most used ways: ... The map.replace() method replaces the entry for the specified key only if it is currently mapped to some value. If we provide a non existing key, the current mapping will ...
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java examples › java: updating keys in a hashmap
Java: Updating Keys in a HashMap - Apps Developer Blog
March 22, 2024 - To gain a deeper understanding ... of HashMap in Java. To update a key in a HashMap, you first need to remove the old key-value pair and then insert a new key-value pair with the updated key....
🌐
Vultr
docs.vultr.com › java › standard library › java › util › hashmap › replace()
Java HashMap replace() - Replace Entry Value
November 19, 2024 - The replace() method in the Java HashMap class provides a powerful means to update the value of a specific key if it already exists in the map.
🌐
HelloKoding
hellokoding.com › modify-a-hashmap-in-java
Add, Update and Remove Key Value Pairs in Java HashMap
October 11, 2020 - Use Java 8+ replace(key, oldValue, newValue) method to replace the specified oldValue with the specified newValue if the specified key has associated with oldValue in the map
🌐
ConcretePage
concretepage.com › java › java-8 › java-map-replace
Java Map replace()
February 15, 2020 - Java 8 The replace is the default method of java.util.Map and has been introduced in Java 8. The replace method replaces the entry value for the specified key only if it is currently mapped to some value.