Java Map replace()

Written by: Arvind Rai,
Last updated:
February 15, 2020
The replace is the default method of java.util.Map and has been introduced in Java 8. The replace method replaces the entry value for the specified key only if it is currently mapped to some value. The replace method uses following arguments.
1. Replaces the value for the specified key.
default V replace(K key, V value) 
The key is the specified key whose associated value needs to change.
The value is the new value to be put.
The replace method returns old value and if there is no associated value with specified key, then it returns null.

2. Replaces the value with new value for the specified key only if the specified old value matches the value associated with specified key.
default boolean replace(K key, V oldValue, V newValue) 
The key is the specified key whose associated value needs to change.
The oldValue is the is old value associated with specified key.
The newValue is the is new value to be put.
The replace method returns true/false whether replacement of value is successful. If specified key has no associated value, then there is no replacement and hence will return false. If already existing old value for the specified key does not match the specified old value, there is no replacement and hence returns false.

Example-1

In this example, we are using HashMap.
Replace1.java
import java.util.HashMap;
import java.util.Map;

public class Replace1 {
  public static void main(String[] args) {
     Map<Integer, String> studentMap = new HashMap<>();
     studentMap.put(101, "Mahesh");
     studentMap.put(102, "Suresh");
     studentMap.put(103, "Krishna");
     
     String oldValue = studentMap.replace(101, "Mr. Mahesh");
     
     System.out.println(oldValue); //Mahesh
     
     System.out.println(studentMap.get(101)); //Mr. Mahesh    
     
     boolean isReplaced = studentMap.replace(102, "Suresh", "Mr. Suresh");
     
     System.out.println(isReplaced); //true
     
     System.out.println(studentMap.get(102)); //Mr. Suresh
     
     isReplaced = studentMap.replace(103, "Krishna11", "Mr. Krishna");
     
     System.out.println(isReplaced); //false
     
     System.out.println(studentMap.get(103)); //Krishna    
  }
} 
Output
Mahesh
Mr. Mahesh
true
Mr. Suresh
false
Krishna 
The same can be achieved without using replace method.
Replace11.java
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Replace11 {
  public static void main(String[] args) {
    Map<Integer, String> studentMap = new HashMap<>();
    studentMap.put(101, "Mahesh");
    studentMap.put(102, "Suresh");
    studentMap.put(103, "Krishna");
   
    int key1 = 101;
    if (studentMap.containsKey(key1)) {
      studentMap.put(key1, "Mr. Mahesh");
    }
    
    System.out.println(studentMap.get(key1));

    int key2 = 102;
    if (studentMap.containsKey(key2) && Objects.equals(studentMap.get(key2), "Suresh")) {
      studentMap.put(102, "Mr. Suresh");
    }    

    System.out.println(studentMap.get(key2));    
 }
} 
Output
Mr. Mahesh
Mr. Suresh 

Example-2

In this example, we are using LinkedHashMap.
Replace2.java
import java.util.LinkedHashMap;
import java.util.Map;

public class Replace2 {
  public static void main(String[] args) {
     Map<Integer, Integer> numberMap = new LinkedHashMap<>();
     numberMap.put(1, 100);
     numberMap.put(2, 200);
     numberMap.put(3, 300);
     
     numberMap.replace(1, numberMap.get(1) + 50); 

     System.out.println(numberMap.get(1)); //150

     numberMap.replace(2, 200, 250);

     System.out.println(numberMap.get(2)); //250
     
     numberMap.replace(3, 301, 350);

     System.out.println(numberMap.get(3)); //300     
  }
} 
Output
150
250
300 

Example-3

In this example, we are using TreeMap.
Replace3.java
import java.util.Map;
import java.util.TreeMap;

public class Replace3 {
  public static void main(String[] args) {
     Map<String, String> treeMap = new TreeMap<>();
     treeMap.put("Bharat", "Modi");
     treeMap.put("Russia", "Putin");
     treeMap.put("USA", "Trump");
     
     String key1 = "Bharat";
     
     treeMap.replace(key1, "Mr. Modi");

     System.out.println(treeMap.get(key1)); //Mr. Modi
     
     String key2 = "Russia";
     
     treeMap.replace(key2, "Putin", "Mr. Putin");

     System.out.println(treeMap.get(key2)); //Mr. Putin
     
     String key3 = "USA";
     
     treeMap.replace(key3, "", "Mr. Trump");

     System.out.println(treeMap.get(key3)); //Trump     
  }
} 
Output
Mr. Modi
Mr. Putin
Trump 

Reference

Java doc: Map

Join the Newsletter

(Subscribe to get our latest content directly into your inbox)

WRITTEN BY
ARVIND RAI
ARVIND RAI









©2024 concretepage.com | Privacy Policy | Contact Us