import java.util.HashMap; import java.util.Map; public class MapExample { public static void main(String[] args) { MapIn this example, we create a HashMap and add three key-value pairs to it. Then we use the replace() method to replace the value of key 2 with "New Two". We print out the old value and the new value of key 2 to verify the replacement. Next, we try to replace the value of key 4, which doesn't exist in the map. Since there is no mapping for key 4, the replace() method returns null and the map remains unchanged. The Map class and the replace() method are part of the Java Collections Framework, which is located in the java.util package.map = new HashMap (); map.put(1, "One"); map.put(2, "Two"); map.put(3, "Three"); // Replace the value of key 2 with "New Two" String oldValue = map.replace(2, "New Two"); System.out.println("Old value of key 2: " + oldValue); System.out.println("New value of key 2: " + map.get(2)); // Try to replace the value of key 4 (which doesn't exist) oldValue = map.replace(4, "Four"); System.out.println("Old value of key 4: " + oldValue); System.out.println("New value of key 4: " + map.get(4)); } }