English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Java HashMap replace() method replaces the mapping of the specified key with the specified new value in the hashmap
The syntax of replace() method is:
hashmap.replace(K key, V oldValue, V newValue)
key - The key to replace its mapping
oldValue (Optional)- The value to be replaced in the mapping
newValue - oldValue is replaced with this value
The HashMap replace() method replaces the mapping and returns:
If the optional parameter oldValue does not exist, it is the previous value associated with the specified key
If the optional parameter oldValue exists, it is true
NoteIf the specified key maps to a null value, or the key is not in the hashmap, this method returns null.
import java.util.HashMap; class Main { public static void main(String[] args) { // Create HashMap HashMap<Integer, String> languages = new HashMap<>(); //Add the entry to HashMap languages.put(1, "Python"); languages.put(2, "English"); languages.put(3, "JavaScript"); System.out.println("HashMap: " + languages); //replace the key2the mapping String value = languages.replace(2, "Java"); System.out.println("The replaced value: " + value); System.out.println("Updated HashMap: " + languages); } }
Output Result
HashMap: {1=Python, 2=English, 3=JavaScript} The replaced value: English Updated HashMap: {1=Python, 2=Java, 3=JavaScript}
In the above example, we created a hash map named languages. Here, we have used the replace() method to replace the key1(1=English) entry with the specified value Java.
Here, the replace() method does not have an optional oldValue parameter. Therefore, it returns the old value (English).
import java.util.HashMap; class Main { public static void main(String[] args) { //Create HashMap HashMap<String, String> countries = new HashMap<>(); //Insert the item into HashMap countries.put("Washington", "America"); countries.put("Ottawa", "Canada"); countries.put("Canberra", "Australia"); System.out.println("Countries:\n" + countries); // Replace the mapping {Washington = America} countries.replace("Washington", "America", "USA"); // returns true countries.replace("Canberra", "New Zealand", "Victoria"); // returns false System.out.println("The Countries after executing replace():\n" + countries); } }
Output Result
Countries: {Canberra=Australia, Ottawa=Canada, Washington=America} The Countries after executing replace(): {Canberra=Australia, Ottawa=Canada, Washington=USA}
In the above example, we created a hash map named countries. Note this line,
countries.replace("Washington", "America", "USA");
Here, the replace() method includes an optional oldValue parameter (America). Therefore, the mapping of the key Washington to the value America is replaced with the new value USA.
But please note that
countries.replace("Canberra", "New Zealand", "Victoria");
Here, the key Canberra is not mapped to the value New Zealand in the hash map. Therefore, the replace() method will not replace any value.
Note:We can use Java HashMap clear() methods remove all mappings from the hash map.
In HashMap, the syntax of put() and replace() methods looks very similar.
// put() syntax hashmap.put(key, value) // replace() syntax hashmap.replace(key, value)
And when the hash map contains the specifiedKeymapping, both methods will replace the value associated with the specified key.
However, if the hash map does not contain the specifiedvalue of the keyFor any mapping, if
The put() method replaces the specifiedKeyandValueInsert a new mapping
The replace() method returns null
import java.util.HashMap; class Main { public static void main(String[] args) { // Create HashMap HashMap<Integer, String> languages1 = new HashMap<>(); // Insert entries into HashMap languages1.put(1, "Python"); languages1.put(2, "JavaScript"); //Create another similar to languages1HashMap HashMap<Integer, String> languages2 = new HashMap<>(); //Remove all entries from language1Insert language2 languages2.putAll(languages1); System.out.println("HashMap: " + languages1); // Using put() languages2.put(3, "Java"); System.out.println("HashMap after put():\n" + languages2); // Using replace() languages1.replace(3, "Java"); System.out.println("HashMap after replace():\n" + languages1); } }
Output Result
HashMap: {1=Python, 2=JavaScript} HashMap after put(): {1=Python, 2=JavaScript, 3=Java} HashMap after HashMap(): {1=Python, 2=JavaScript}
In the above example, we created two named languages1and languages2of the hash map. We usedHashMap putAll()methods so that two hash maps have the same mappings.
Here, there is no key in the hash map3. Therefore,
The put() method adds a new mapping(3 = Java) addto HashMap
The replace() method does nothing.
AccessJava HashMap put()methods to learn more about adding entries.