Java Map replace() replaceAll() Methods Tutorial

In this section we will learn what the Map replace() and replaceAll() methods are and how to use them in Java.

What is Java Map replace() Method?

The Java Map replace() method is used to replace the value of a key in a Map object with a new one.

Note: this method only works if the specified key in the map is already there and has a non-null value associated with it.

Java replace() Method Syntax:

default V replace(K key, V value)

default boolean replace(K key, V oldValue, V newValue)

replace() Method Parameters:

The method has two variants:

For the first variant, it takes two arguments:

  • The first argument is the key we want to replace its value with a new one.
  • The second argument is the new value that we want to replace the old value in the map object with it.

For the second variant, the method takes three arguments:

  • The first argument is the key that we want to replace its value.
  • The second argument is the expected value (AKA old value) for the key.
  • The third argument is the new value that we want to replace the old value with it in the target map object.

replace() Method Return Value:

For the first variant of the method, the return value is the old value (the replaced value) of the target key in the map object.

For the second variant of the method, the return value is of type Boolean. If the operation was successful, then the return value of the method will be true, otherwise the value false will return instead.

replace() Method Exceptions:

UnsupportedOperationException: We get this exception if the method is not supported by the target Map object.

ClassCastException: We get this exception if the datatype of the key or value is of incompatible type compared to the key or value in the target Map object.

NullPointerException: We get this exception if we put a null argument for either key or value and the target Map object doesn’t permit a null value.

IllegalArgumentException: We get this exception if the key or value has a property that doesn’t allow it to be an element in a Map object.

Example: using Map replace() method

import java.util.Map; 
import java.util.HashMap;
class Main{
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("Omid","Doe");
        map.put("Jack","Bauer");
        map.put("Barack","Obama");
        map.put("Elon","Musk");
        map.put("Lex","Fridman");

        map.replace("Omid","Dehghan");
        map.forEach((k,v)->{
            System.out.println(k+" "+v);
        });
    }
}

Output:

Elon Musk

Barack Obama

Omid Dehghan

Jack Bauer

Lex Fridman

What is Java Map replaceAll() Method?

The Java Map replaceAll() method is used to run a set of instructions on each entry of a Map object and replace the value in that entry as a result of running the instructions.

Basically, this method takes a reference to a method and runs that method for every entry in the target Map object. The return value of this reference method is the new value that will replace the old value of the current entry.

Java replaceAll() Method Syntax:

default void replaceAll(BiFunction<? super K,? super V,? extends V> function)

replaceAll() Method Parameters:

The method takes one parameter and that is of type BiFunction interface, which is a functional one.

Basically, this means we need to pass a reference of a method as the argument of the replaceAll method.

The signature of this method is:

  • It takes two arguments. The data type of the first argument should be the same as the data type of the keys in the target map object and the second argument also should have the same data type as the values in the target map object.
  • The return value of this reference method is of the same data type as the values of the target Map object.

Note: we can create this method using the lambda expression, or refer to an instance or static method that is already in our program and has the same signature.

replaceAll() Method Return Value:

The return value of the method is void.

But the return value of the reference method (the one that we put as the argument of this method) is the new value that will replace the old value of the entry that this method is called for.

replaceAll() Method Exceptions:

UnsupportedOperationException: We get this exception if the method is not supported by the target Map object.

ClassCastException: We get this exception if the datatype of the value is of incompatible type compared to the value in the target Map object.

NullPointerException: We get this exception if we put a null argument for the value or reference function.

IllegalArgumentException: We get this exception if the value has a property that doesn’t allow it to be an element in a Map object.

ConcurrentModificationException: We get this exception if an entry is found to be removed during iteration.

Example: using Map replaceAll() method

import java.util.Map; 
import java.util.HashMap;
class Main{
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("Omid","Doe");
        map.put("Jack","Bauer");
        map.put("Barack","Obama");
        map.put("Elon","Musk");
        map.put("Lex","Fridman");

        map.replaceAll((k,v)->{
            return "Doe";
        });
        map.forEach((k,v)->{
            System.out.println(k+" "+v);
        });
    }
}

Output:

Elon Doe

Barack Doe

Omid Doe

Jack Doe

Lex Doe
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies