A method is a group of code or set of code which is used perform a certain task. We do not need to write code again and again , instead of writing the code we can use these methods. There are two types of methods in java ‘user defined’ and ‘Inbuilt’ methods. The Hashmap class in java also have some inbuilt methods, one of those method is Java Hashmap replace() Method.
To know more about Hashmap replace() Method read the complete article
Java Hashmap replace() Method
The hashmap replace() method is used to replace the value mapped to the specified key. This method returns the old value and map the new value to the key. Below in this page you can find it’s syntax, return values, parameters with detailed examples.
Syntax :
hashmap.replace(Object key , Object value);
ParametersThis method takes one parameter. Object key : Key whose mapping value is to be replace in the hashmap.Object value : new value which is to be mapped with the key.
Return values :
Value which is mapped to the specified key. null : If the key is not present in the hashmap.
import java.util.*;
public class Main
{
public static void main (String[]args)
{
// Creating an empty HashMap
HashMap < Integer, String > hash_map_1 =
new HashMap < Integer, String > ();
// Mapping string values to int keys in a empty hashmap
hash_map_1.put (10, "Java");
hash_map_1.put (20, "Hashmap");
hash_map_1.put (25, "Replace");
hash_map_1.put (30, "method");
//replacing the value mapped to key 20
String replaced= hash_map_1.replace(20,"Python");
System.out.println ("old value mapped to key 20 is : " + replaced);
System.out.println("new value mapped to key 20 is : "+ hash_map_1.get(20));
}
}
Output :
old value mapped to key 20 is : Hashmap
new value mapped to key 20 is : Python
What happened above : In the above example a hashmap is created, initially which is empty and elements are inserted in hash_map_1. hash_map_1.replace(20,"Python") is called which gives 'Hashmap' as return value and mapp the new value "Python" to the key.
import java.util.*;
public class Main
{
public static void main (String[]args)
{
// Creating an empty HashMap
HashMap < Integer, String > hash_map_1 =
new HashMap < Integer, String > ();
// Mapping string values to int keys in a empty hashmap
hash_map_1.put (10, "Java");
hash_map_1.put (20, "Hashmap");
hash_map_1.put (25, "Remove");
hash_map_1.put (30, "method");
//replacing the value mapped to key 35
String replaced= hash_map_1.replace(35,"Python");
System.out.println ("old value mapped to key 35 is : " + replaced);
System.out.println("new value mapped to key 35 is : "+ hash_map_1.get(35));
}
}
Output :
old value mapped to key 35 is : null
new value mapped to key 35 is : null
What happened above : In the above example a hashmap is created and key/values pair are inserted . Then hash_map_1.replace(35."Python") is called which gives null as return value because the key 35 is not mapped to any value in tha hash_map_1.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment