Java Hashmap replace() Method

hashmap replace method

Methods in Java

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);

Return values :

Value which is mapped to the specified key.
null : If the key is not present in the hashmap.
Java Hashmap replace Method

Hashmap replace() Method Examples

Example 1 :

Run
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

Example 2 :

Run
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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription