HashMap replace() Method in Java

Published by user on

The replace(K key, V value) method of HashMap replaces an entry for the specified key. It returns replaced value. It returns null if the map does not contain an entry for the specified key.

We can also use the replace(K key, V oldValue, V newValue) method to replace the old value with a new value. It returns true on successful replacement, otherwise, it returns false.

Method Signatures

public Object replace(K key, V value)
 public Object replace(K key,V oldValue, V newValue) 

Program 1

import java.util.HashMap;
import java.util.Map;

public class HashMapReplaceExample {

    public static void main(String[] args) {

        Map < Integer, String > map = new HashMap < > ();
        map.put(101, "John");
        map.put(102, "Adam");
        map.put(103, "Ricky");
        map.put(104, "Chris");

        System.out.println("Elements: " + map);

        // replace John with Shaun
        // replace method returns the old value
        String oldValue = map.replace(101, "Shuan");

        System.out.println("Replaced John by Shaun for key 101: " + oldValue);
        System.out.println("Modified elements: " + map);

        // returns null if map 
        // does not contain specified key
        System.out.println("No key is present for 108: " + map.replace(108, "Gary"));
    }
}

Output

Elements: {101=John, 102=Adam, 103=Ricky, 104=Chris}
Replaced John by Shaun for key 101: John
Modified elements: {101=Shuan, 102=Adam, 103=Ricky, 104=Chris}
No key is present for 108: null

Explanation

  • Declare and initialize HashMap
  • Put values John, Adam, Ricky, and Chris to the HashMap
  • Call replace method to replace ‘John’ with ‘Shaun’
  • Call replace method for non-existent key 108, this returns null as the key is not present

Program 2

import java.util.HashMap;
import java.util.Map;

public class HashMapReplaceExample {

    public static void main(String[] args) {

        Map < Integer, String > map = new HashMap < > ();
        map.put(101, "John");
        map.put(102, "Adam");
        map.put(103, "Ricky");
        map.put(104, "Chris");

        System.out.println("Elements: " + map);

        System.out.println(map.replace(103, "Ricky", "Michale"));
        System.out.println("Modified elements: " + map);

        System.out.println(map.replace(104, "Joe", "Michale"));
        // returns false if the map 
        // does not contain secified 
        // key-value pair(104,"Joe")
    }
}

Output

Elements: {101=John, 102=Adam, 103=Ricky, 104=Chris}
true
Modified elements: {101=John, 102=Adam, 103=Michale, 104=Chris}
false

Explanation

  • Declare and initialize HashMap
  • Put values John, Adam, Ricky, and Chris to the HashMap
  • Try to replace (103, Ricky) with Michale. This returns true as the key and value are present
  • Now, try to replace (104, Joe) with Michale. The key is present but the value is not present. Hence, the replacement is not possible
Categories: Java

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *