The replace(K key, V value) method of Map interface, implemented by HashMap class is used to replace the value of the specified key only if the key is previously mapped with some value.
Syntax:
public V replace(K key, V value)
Parameters: This method accepts two parameters:
- key: which is the key of the element whose value has to be replaced.
- value: which is the new value which has to be mapped with the provided key.
Return Value: This method returns the previous value associated with the specified key. If there is no such key mapped, then it returns null, if the implementation supports null value.
Exceptions: This method will throw:
- NullPointerException if the specified key or value is null, and this map does not permit null keys or values and
- IllegalArgumentException if some property of the specified key or value prevents it from being stored in this map.
Program 1:
// Java program to demonstrate // replace(K key, V value) method import java.util.*;
public class GFG {
// Main method
public static void main(String[] args)
{
// Create a HashMap and add some values
HashMap<String, Integer> map
= new HashMap<>();
map.put( "a" , 100 );
map.put( "b" , 300 );
map.put( "c" , 300 );
map.put( "d" , 400 );
// print map details
System.out.println( "HashMap: "
+ map.toString());
// provide value for the key which has
// to replace it's current value,
// using replace(K key, V value) method
map.replace( "b" , 200 );
// print new mapping
System.out.println( "New HashMap: "
+ map.toString());
}
} |
HashMap: {a=100, b=300, c=300, d=400} New HashMap: {a=100, b=200, c=300, d=400}
Program 2:
// Java program to demonstrate // replace(K key, V value) method import java.util.*;
public class GFG {
// Main method
public static void main(String[] args)
{
// Create a HashMap and add some values
HashMap<String, Integer> map
= new HashMap<>();
map.put( "a" , 100 );
map.put( "b" , 300 );
map.put( "c" , 300 );
map.put( "d" , 400 );
// print map details
System.out.println( "HashMap: "
+ map.toString());
// provide value for the key which has
// to replace it's current value, and will
// store the value in k using the
// replace(K key, V value) method
int k = map.replace( "b" , 200 );
// print the value of k
System.out.println( "Previous value of 'b': "
+ k);
// print new mapping
System.out.println( "New HashMap: "
+ map.toString());
}
} |
HashMap: {a=100, b=300, c=300, d=400} Previous value of 'b': 300 New HashMap: {a=100, b=200, c=300, d=400}
Program 3:
// Java program to demonstrate // replace(K key, V value) method import java.util.*;
public class GFG {
// Main method
public static void main(String[] args)
{
// Create a HashMap and add some values
HashMap<String, Integer> map
= new HashMap<>();
map.put( "a" , 100 );
map.put( "b" , 300 );
map.put( "c" , 300 );
map.put( "d" , 400 );
// print map details
System.out.println( "HashMap: "
+ map.toString());
// provide value for the key which is
// not mapped previously and store the
// return value in Integer k, using
// replace(K key, V value) method
Integer k = map.replace( "e" , 200 );
// print the value of k
System.out.println( "Value of k, returned "
+ "for key 'e': " + k);
// print new mapping
System.out.println( "New HashMap: "
+ map.toString());
}
} |
HashMap: {a=100, b=300, c=300, d=400} Value of k, returned for key 'e': null New HashMap: {a=100, b=300, c=300, d=400}
References: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#replace-K-V-