Write a Java Program to Update value of HashMap using key

HashMap is a data structure in Java that stores key-value pairs.

It allows us to store and retrieve values based on their corresponding keys.

HashMaps are widely used in programming, and it’s important to know how to update the value of a HashMap using a key.

In this tutorial, we will learn how to update the value of a HashMap using a key in Java.


Before we start, let’s first create a HashMap object and add some values to it:

HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);

Now, let’s say we want to update the value of “apple” to 5.

To do that, we can use the put() method of the HashMap class:

map.put("apple", 5);

This will update the value of “apple” to 5. If the key “apple” already exists in the HashMap, the put() method will update its value.

If the key does not exist, the put() method will add a new key-value pair to the HashMap.

We can also use the replace() method of the HashMap class to update the value of a key.

The replace() method takes two arguments: the key whose value we want to update, and the new value.

map.replace("apple", 5);

This will also update the value of “apple” to 5.

If we want to update the value of a key only if it already exists in the HashMap, we can use the replace() method with an additional parameter:

map.replace("apple", 1, 5);

This will update the value of “apple” to 5 only if its current value is 1.


In conclusion, updating the value of a HashMap using a key is easy in Java.

We can use the put() method or the replace() method of the HashMap class to update the value of a key.

It’s important to note that when updating the value of a key in a HashMap, the key remains the same.

If we try to update the value of a key that does not exist in the HashMap, a new key-value pair will be added to the HashMap.

Also, if we try to update the value of a key in a HashMap that has been declared as final, we will get a compile-time error.

This is because the final keyword makes the HashMap immutable, and we cannot modify its contents once it has been initialized.

In addition to the methods we have discussed, the HashMap class provides several other methods that can be used to update the values of keys.

Some of these methods include putIfAbsent(), merge(), and computeIfPresent().

These methods provide different ways of updating the values of keys in a HashMap and are useful in different scenarios.

In summary, updating the value of a HashMap using a key is a common operation in Java programming.

We can use the put() or replace() method of the HashMap class to update the value of a key.

It’s important to keep in mind that the key remains the same when updating the value, and that the HashMap must not be declared as final if we want to modify its contents.

By understanding these concepts and utilizing the appropriate methods, we can easily update the values of keys in a HashMap in Java.