The syntax of the replace() method is:
hashmap.replace(K key, V oldValue, V newValue)
Here, hashmap is an object of the HashMap class.
replace() Parameters
The replace() method can take 3 parameters.
- key - key whose mapping is to be replaced
- oldValue (optional)- value to be replaced in the mapping
- newValue - oldValue is replaced with this value
replace() Return Values
The HashMap replace() method replaces the mapping and returns:
- the previous value associated with the specified key, if the optional parameter oldValue is not present
true, if the optional parameter oldValue is present
Note: The method returns null, if either the specified key is mapped to a null value or the key is not present on the hashmap.
Example 1: Replace an Entry in HashMap
Output
HashMap: {1=Python, 2=English, 3=JavaScript}
Replaced Value: English
Updated HashMap: {1=Python, 2=Java, 3=JavaScript}
In the above example, we have created a hashmap named languages. Here, we have used the replace() method to replace the entry for key 1 (1=English) with the specified value Java.
Here, the replace() method does not have the optional oldValue parameter. Hence, it returns the old value (English).
Example 2: HashMap replace() with Old Value
Output
Countries:
{Canberra=Australia, Ottawa=Canada, Washington=America}
Countries after replace():
{Canberra=Australia, Ottawa=Canada, Washington=USA}
In the above example, we have created a hashmap named countries. Notice the line,
countries.replace("Washington", "America", "USA");
Here, the replace() method includes the optional oldValue parameter (America). Hence, the mapping where key Washington maps to value America is replaced with new value USA.
However, notice the line,
countries.replace("Canberra", "New Zealand", "Victoria");
Here, in the hashmap, the key Canberra does not map to value New Zealand. Hence, the replace() method does not replace any value.
Note: We can use the Java HashMap clear() method to remove all the mappings from the hashmap.
HashMap put() Vs. replace()
The syntax of the put() and replace() method looks quite similar in HashMap.
// syntax of put()
hashmap.put(key, value)
// syntax of replace()
hashmap.replace(key, value)
And, when the hashmap contains the mapping for the specified key, then both the methods replace the value associated with the specified key.
However, if the hashmap does not contain any mapping for the specified key, then
- the
put()method inserts the new mapping for the specified key and value - the
replace()method returnsnull
Example 3: HashMap put() Vs. replace()
Output
HashMap: {1=Python, 2=JavaScript}
HashMap after put():
{1=Python, 2=JavaScript, 3=Java}
HashMap after replace():
{1=Python, 2=JavaScript}
In the above example, we have created two hashmaps named languages1 and languages2. We have used the HashMap putAll() method so that both hashmaps have the same mappings.
Here, the mapping for key 3 is not present in the hashmap. Hence,
- the
put()method adds the new mapping (3 = Java) toHashMap - the
replace()method does not perform any operation
To learn more about adding entries, visit Java HashMap put().