Java HashMap replace()

The syntax of the replace() method is:

hashmap.replace(K key, V oldValue, V newValue)

Here, hashmap is an object of the HashMap class.


replace() Parameters

The replace() method can take 3 parameters.

  • key - key whose mapping is to be replaced
  • oldValue (optional)- value to be replaced in the mapping
  • newValue - oldValue is replaced with this value

replace() Return Values

The HashMap replace() method replaces the mapping and returns:

  • the previous value associated with the specified key, if the optional parameter oldValue is not present
  • true, if the optional parameter oldValue is present

Note: The method returns null, if either the specified key is mapped to a null value or the key is not present on the hashmap.


Example 1: Replace an Entry in HashMap

import java.util.HashMap;

class Main {
  public static void main(String[] args) {

    // create an HashMap
    HashMap<Integer, String> languages = new HashMap<>();

    // add entries to HashMap
    languages.put(1, "Python");
    languages.put(2, "English");
    languages.put(3, "JavaScript");
    System.out.println("HashMap: " + languages);

    // replace mapping for key 2
    String value = languages.replace(2, "Java");

    System.out.println("Replaced Value: " + value);
    System.out.println("Updated HashMap: " + languages);
  }
}

Output

HashMap: {1=Python, 2=English, 3=JavaScript}
Replaced Value: English
Updated HashMap: {1=Python, 2=Java, 3=JavaScript}

In the above example, we have created a hashmap named languages. Here, we have used the replace() method to replace the entry for key 1 (1=English) with the specified value Java.

Here, the replace() method does not have the optional oldValue parameter. Hence, it returns the old value (English).


Example 2: HashMap replace() with Old Value

import java.util.HashMap;

class Main {
  public static void main(String[] args) {
    // create an HashMap
    HashMap<String, String> countries = new HashMap<>();

    // insert items to the HashMap
    countries.put("Washington", "America");
    countries.put("Ottawa", "Canada");
    countries.put("Canberra", "Australia");
    System.out.println("Countries:\n" + countries);

    // replace mapping {Washington = America}
    countries.replace("Washington", "America", "USA");  // return true

    countries.replace("Canberra", "New Zealand", "Victoria");  // return false

    System.out.println("Countries after replace():\n" + countries);
  }
}

Output

Countries:
{Canberra=Australia, Ottawa=Canada, Washington=America}
Countries after replace():
{Canberra=Australia, Ottawa=Canada, Washington=USA}

In the above example, we have created a hashmap named countries. Notice the line,

countries.replace("Washington", "America", "USA");

Here, the replace() method includes the optional oldValue parameter (America). Hence, the mapping where key Washington maps to value America is replaced with new value USA.

However, notice the line,

countries.replace("Canberra", "New Zealand", "Victoria");

Here, in the hashmap, the key Canberra does not map to value New Zealand. Hence, the replace() method does not replace any value.

Note: We can use the Java HashMap clear() method to remove all the mappings from the hashmap.


HashMap put() Vs. replace()

The syntax of the put() and replace() method looks quite similar in HashMap.

// syntax of put()
hashmap.put(key, value)

// syntax of replace()
hashmap.replace(key, value)

And, when the hashmap contains the mapping for the specified key, then both the methods replace the value associated with the specified key.

However, if the hashmap does not contain any mapping for the specified key, then

  • the put() method inserts the new mapping for the specified key and value
  • the replace() method returns null

Example 3: HashMap put() Vs. replace()

import java.util.HashMap;

class Main {
  public static void main(String[] args) {

    // create an HashMap
    HashMap<Integer, String> languages1 = new HashMap<>();

    // insert entries to HashMap
    languages1.put(1, "Python");
    languages1.put(2, "JavaScript");

    // create another HashMap similar to languages1
    HashMap<Integer, String> languages2 = new HashMap<>();

    // puts all entries from languages1 to languages2
    languages2.putAll(languages1);
    System.out.println("HashMap: " + languages1);

    // use of put()
    languages2.put(3, "Java");
    System.out.println("HashMap after put():\n" + languages2);

    // use of replace()
    languages1.replace(3, "Java");
    System.out.println("HashMap after replace():\n" + languages1);

  }
}

Output

HashMap: {1=Python, 2=JavaScript}
HashMap after put():
{1=Python, 2=JavaScript, 3=Java}
HashMap after replace():
{1=Python, 2=JavaScript}

In the above example, we have created two hashmaps named languages1 and languages2. We have used the HashMap putAll() method so that both hashmaps have the same mappings.

Here, the mapping for key 3 is not present in the hashmap. Hence,

  • the put() method adds the new mapping (3 = Java) to HashMap
  • the replace() method does not perform any operation

To learn more about adding entries, visit Java HashMap put().

Did you find this article helpful?