Java HashMap replace() Method (original) (raw)

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);
}

}

`

Output

Original 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:

**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);
}

}

`

Output

Original 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);
}

}

`

Output

Original 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);
}

}

`

Output

Original map: {1=Geek1, 3=Geek3} Replaced value: Geek3 Updated map: {1=Geek1, 3=Geek30}