Friday, October 21, 2022

Java Map replace() With Examples

The replace() method in java.util.Map is added in Java 8. The replace() method replaces the value associated with the specified key only if the key is already mapped to some value. That's how replace() method differs from put() method, if a key is passed to the put() method that doesn't already exist in the Map then it will add the entry (key, value pair) to the Map.

Replace method in Java Map

There are two overloaded replace methods

1. V replace(K key, V value)

Replaces the entry for the specified key only if it is currently mapped to some value.

Paramters are-

  • key- The specified key for which value has to be replaced
  • value- New value to be associated with the specified key

Method returns the previous value if the key is mapped to a value. Returns null if key is not associated with any value, returns null if associated values is null for the key.

2. boolean replace(K key, V oldValue, V newValue)

Replaces the oldValue with newValue for the specified key only if the key is currently mapped to the oldValue.

  • key- Specified key for which value has to be changed
  • oldValue- Value expected to be associated with the specified key
  • newValue- Value that replaces the oldValue

Method returns true if the value was replaced, false otherwise.

replace() method Java examples

In this example we'll use replace(K key, V value) variant.

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

public class ReplaceDemo {
  public static void main(String[] args) {
    // Setting up a HashMap
    Map<String, String> cityMap = new HashMap<String, String>();
    cityMap.put("1","New York City" );
    cityMap.put("2", "New Delhi");
    cityMap.put("3", "Mumbai");
    cityMap.put("4", "Berlin");
    
    System.out.println("*** Map Initially ***");
    System.out.println(cityMap);
    String prevValue = cityMap.replace("3", "Chennai");
    System.out.println("Value that is replaced " + prevValue);
    
    prevValue = cityMap.replace("5", "Mumbai");
    System.out.println("Value that is replaced " + prevValue);
    
    System.out.println("*** Map After replacement ***");
    System.out.println(cityMap);
  }
}

Output

*** Map Initially ***
{1=New York City, 2=New Delhi, 3=Mumbai, 4=Berlin}
Value that is replaced Mumbai
Value that is replaced null
*** Map After replacement ***
{1=New York City, 2=New Delhi, 3=Chennai, 4=Berlin}

When "3" is passed as key in the replace() method the passed value replaces the previous value. In this case replace() method returns previous value associated with the key.

When "5" is passed as key there is no change in the Map as the key is not present in the HashMap. In this case replace() method returns null.

In this example we'll use replace(K key, V oldValue, V newValue) variant.

public class ReplaceDemo {

  public static void main(String[] args) {
    // Setting up a HashMap
    Map<String, String> cityMap = new HashMap<String, String>();
    cityMap.put("1","New York City" );
    cityMap.put("2", "New Delhi");
    cityMap.put("3", "Mumbai");
    cityMap.put("4", "Berlin");
    
    System.out.println("*** Map Initially ***");
    System.out.println(cityMap);
    boolean isReplaced = cityMap.replace("3", "Mumbai", "Chennai");
    System.out.println("Is value replaced " + isReplaced);
    
    isReplaced = cityMap.replace("4", "Mumbai", "Madrid");
    System.out.println("Is value replaced " + isReplaced);
    
    System.out.println("*** Map After replacement ***");
    System.out.println(cityMap);
  }
}

Output

*** Map Initially ***
{1=New York City, 2=New Delhi, 3=Mumbai, 4=Berlin}
Is value replaced true
Is value replaced false
*** Map After replacement ***
{1=New York City, 2=New Delhi, 3=Chennai, 4=Berlin}

First time value is replaced because key "3" was mapped with "Mumbai" initially. In this case replace() method returns true.

Second time there is no replacement because key "4" is there but it is not associated with "Mumbai". In this case replace() method returns false.

That's all for this topic Java Map replace() With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. How to Remove Entry From HashMap in Java
  2. Java Map putIfAbsent() With Examples
  3. How HashMap Works Internally in Java
  4. HashSet Vs LinkedHashSet Vs TreeSet in Java
  5. Difference Between Comparable and Comparator in Java

You may also like-

  1. How to Convert Array to ArrayList in Java
  2. Java Phaser With Examples
  3. Java Abstract Class and Abstract Method
  4. java.lang.UnsupportedClassVersionError - Resolving UnsupportedClassVersionError in Java
  5. Convert double to int in Java
  6. Spring Setter Based Dependency Injection
  7. BeanFactoryAware Interface in Spring Framework
  8. FormGroup in Angular With Examples

No comments:

Post a Comment