Java HashMap replace() Method
Last Updated :
22 Jan, 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.
Note: If the key does not exist, the method does nothing and the map remains unchanged.
Example 1: This example demonstrates replacing the value of an existing key in the HashMap.
Java
// Java program to demonstrate the working of replace()
import java.util.HashMap;
public class Geeks {
public static void main(String[] args) {
// Create a HashMap
HashMap<Integer, String> hm = new HashMap<>();
hm.put(1, "Geek1");
hm.put(2, "Geek2");
hm.put(3, "Geek3");
System.out.println("Original map: " + hm);
// Replace the value for key 2
String oldValue = hm.replace(2, "Geek10");
System.out.println("Replaced value: " + oldValue);
System.out.println("Updated map: " + hm);
}
}
OutputOriginal map: {1=Geek1, 2=Geek2, 3=Geek3}
Replaced value: Geek2
Updated map: {1=Geek1, 2=Geek10, 3=Geek3}
Syntax of HashMap replace() Method
public V replace(K key, V newValue)
Parameters:
- key: The key whose associated value is to be replaced.
- value: The new value to associate with the specified key.
Return Type: This method returns the old value associated with the key or returns null if the key does not exist in the map.
Example 2: This example demonstrates that if the key does not exist in the HashMap, the replace() method does nothing and returns null.
Java
// Jav Program to handle non-existent key
import java.util.HashMap;
public class Geeks {
public static void main(String[] args) {
// Create a HashMap
HashMap<Integer, String> hm = new HashMap<>();
hm.put(1, "Geek1");
hm.put(4, "Geek4");
System.out.println("Original map: " + hm);
// Trying to replace value for
// a non-existing key (key 2)
String oldValue = hm.replace(2, "Geek10");
System.out.println("Replaced value: " + oldValue);
System.out.println("Updated map: " + hm);
}
}
OutputOriginal map: {1=Geek1, 4=Geek4}
Replaced value: null
Updated map: {1=Geek1, 4=Geek4}
Example 3: This example demonstrates using the overloaded replace() method to conditionally replace a value only if the existing value matches a specific value.
Java
// Java Program to demonstrate
// conditional value replacement
import java.util.HashMap;
public class Geeks {
public static void main(String[] args) {
// Create a HashMap
HashMap<Integer, String> hm = new HashMap<>();
hm.put(1, "Geek1");
hm.put(2, "Geek2");
System.out.println("Original map: " + hm);
// Replace value for key 2 only if
// the current value is "Geek2"
boolean b = hm.replace(2, "Geek2", "Geek10");
System.out.println("Was value replaced? " + b);
System.out.println("Updated map: " + hm);
}
}
OutputOriginal map: {1=Geek1, 2=Geek2}
Was value replaced? true
Updated map: {1=Geek1, 2=Geek10}
Example 4: This example shows checking if a key exists before calling replace().
Java
// Checking before replacement
import java.util.HashMap;
public class Geeks {
public static void main(String[] args) {
// Create a HashMap
HashMap<Integer, String> hm = new HashMap<>();
hm.put(1, "Geek1");
hm.put(3, "Geek3");
System.out.println("Original map: " + hm);
// Check if key exists
if (hm.containsKey(3)) {
String oldValue = hm.replace(3, "Geek30");
System.out.println("Replaced value: " + oldValue);
} else {
System.out.println("Key 3 does not exist.");
}
System.out.println("Updated map: " + hm);
}
}
OutputOriginal map: {1=Geek1, 3=Geek3}
Replaced value: Geek3
Updated map: {1=Geek1, 3=Geek30}
Similar Reads
Java HashMap remove() Method The remove() method of the Java HashMap class is used to remove a key-value pair from the map based on the given key. If the key is found, the mapping associated with the key is returned, otherwise, it returns null.Example 1: This example demonstrates how to use remove(Object key) to remove a key-va
3 min read
Java HashMap put() Method The put() method of the Java HashMap class is used to add or update the key-value pairs in the map. If the key already exists in the map, the previous value associated with the key is replaced by the new value and If the key does not exist, the new key-value pair is added to the map.Syntax of HashMa
1 min read
Java HashMap putAll() Method The putAll() method of the Java HashMap class is used to copy all key-value mappings from another map to the existing map. If a key exists in both maps, its value is updated with the value from the source map. Otherwise, new key-value pairs are added.Example 1: This example demonstrates copying all
2 min read
HashMap values() Method in Java The java.util.HashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap. Syntax: Hash_Map.values() Parameters: The method does not accept any parameters. Return Value: The method i
2 min read
Java HashMap putIfAbsent() Method The putIfAbsent() method of the HashMap class in Java is used to insert a key-value pair into the map only if the key is not present in the map or is mapped to null. The existing value remains unchanged if the key is already associated with a value.Example 1: In this example, we will demonstrate the
3 min read
Java HashMap merge() Method In Java, the merge() method of the HashMap class is used to either add new key-value pairs or update the existing one in the map by merging values using a specified function.If the key is not present in the map, the merge() method adds the key with the given value.If the key already exists in the ma
3 min read