Java HashMap replace(key, oldValue, newValue) Method
Last Updated : 22 Jan, 2025
Improve
The replace(key, oldValue, newValue) method of the HashMap class in Java is used to conditionally replace the value of a specified key if, and only if, the current value matches the specified old value.
Example 1: This example demonstrates replacing a value only if the key is mapped to the given old value.
// Java program demonstrates 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 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);
}
}
Output
Original map: {1=Geek1, 2=Geek2, 3=Geek3} Was value replaced? true Updated map: {1=Geek1, 2=Geek10, 3=Geek3}
Syntax of replace(key, oldValue, newValue) Method
boolean replace(K key, V oldValue, V newValue)
Parameters:
- key: The key whose associated value is to be replaced.
- oldValue: The expected current value associated with the key.
- newValue: The new value to associated with the key.
Return Type:
This method returns “true” if the value associated with the key is replaced successfully or return “false” if the key does not exist in the map.
Exception:
- NullPointerException occurs only if null is used for a key or value in a map that doesn’t support null.
- IllegalArgumentException only applies to certain Map implementations (like some custom implementations or ConcurrentHashMap).
Example 2: This example shows what happens when the old value provided does not match the current value.
// Java program demonstrates mismatched old value
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, "Java");
hm.put(2, "C++");
hm.put(3, "Python");
System.out.println("Initial HashMap: " + hm);
// Replace value for key 2
// if old value matches
boolean b = hm.replace(2, "JavaScript", "C#");
System.out.println("Was replacement successful? "
+ b);
System.out.println("Updated HashMap: " + hm);
}
}
Output
Initial HashMap: {1=Java, 2=C++, 3=Python} Was replacement successful? false Updated HashMap: {1=Java, 2=C++, 3=Python}
Example 3: This example demonstrates that if the key does not exists, the method returns false.
// Java program demonstrates what happens
// if the key does not exists
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, "Java");
hm.put(2, "C++");
System.out.println("Initial HashMap: " + hm);
// Replace value for key 2
// if old value matches
boolean b = hm.replace(3, "JavaScript", "C#");
System.out.println("Was replacement successful? "
+ b);
System.out.println("Updated HashMap: " + hm);
}
}
Output
Initial HashMap: {1=Java, 2=C++} Was replacement successful? false Updated HashMap: {1=Java, 2=C++}