In this guide, you will learn about the HashMap replace() method in Java programming and how to use it with an example.
1. HashMap replace() Method Overview
Definition:
The replace() method of the HashMap class in Java is used to replace the value associated with a specific key in the map, only if the key is already associated with some value. The method comes in several overloaded forms, allowing replacing the value conditionally based on the current associated value.
Syntax:
1. public V replace(K key, V value)
2. public boolean replace(K key, V oldValue, V newValue)
Parameters:
1. For the first form:
- key: The key with which the specified value is associated.
- value: The value to be associated with the specified key.
2. For the second form:
- key: The key with which the specified value is associated.
- oldValue: The value expected to be associated with the specified key.
- newValue: The value to be associated with the specified key.
Key Points:
- The replace() method will replace the value associated with the specified key only if the key is already present in the map.
- If the key is not present in the map, the method does nothing and returns null (for the first form) or false (for the second form).
- The second form of the method replaces the entry for the specified key only if currently mapped to the specified value.
- If the replacement is successful, the method returns the old value (for the first form) or true (for the second form).
2. HashMap replace() Method Example
import java.util.HashMap;
public class HashMapReplaceExample {
public static void main(String[] args) {
HashMap<String, String> capitalMap = new HashMap<>();
// Putting some key-value pairs in the HashMap
capitalMap.put("USA", "Washington DC");
capitalMap.put("Germany", "Berlin");
// Printing the original HashMap
System.out.println("Original HashMap: " + capitalMap);
// Using replace() - Key exists in the HashMap
String returnedValue1 = capitalMap.replace("USA", "New York");
System.out.println("Returned Value (USA): " + returnedValue1);
// Using replace() - Key does not exist in the HashMap
String returnedValue2 = capitalMap.replace("France", "Paris");
System.out.println("Returned Value (France): " + returnedValue2);
// Using conditional replace() - oldValue matches the current value
boolean isReplaced1 = capitalMap.replace("Germany", "Berlin", "Munich");
System.out.println("Is Germany replaced? " + isReplaced1);
// Using conditional replace() - oldValue does not match the current value
boolean isReplaced2 = capitalMap.replace("Germany", "Berlin", "Hamburg");
System.out.println("Is Germany replaced? " + isReplaced2);
// Printing the updated HashMap
System.out.println("Updated HashMap: " + capitalMap);
}
}
Output:
Original HashMap: {USA=Washington DC, Germany=Berlin} Returned Value (USA): Washington DC Returned Value (France): null Is Germany replaced? true Is Germany replaced? false Updated HashMap: {USA=New York, Germany=Munich}
Explanation:
In this example, we initially have a HashMap with some key-value pairs.
We demonstrate using both forms of the replace() method. When using the first form, if the key exists, the method replaces the value associated with the key and returns the old value; if the key does not exist, the method returns null.
The second form conditionally replaces the value associated with the key only if it is currently mapped to a specified value; it returns true if a replacement was successful and false otherwise.
The output showcases the values returned by the method and the state of the HashMap before and after using the replace() method.
Comments
Post a Comment
Leave Comment