Thursday, July 4, 2024
HomeLanguagesJavaIdentityHashMap put() Method in Java

IdentityHashMap put() Method in Java

The java.util.IdentityHashMap.put() method of IdentityHashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping, into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole.

Syntax:

Identity_Hash_Map.put(key, value)

Parameters: The method takes two parameters, both are of the Object type of the IdentityHashMap.

  • key: This refers to the key element that needs to be inserted into the Map for mapping.
  • value: This refers to the value that the above key would map into.

Return Value: If an existing key is passed then the previous value gets returned. If a new pair is passed, then NULL is returned.

Below programs are used to illustrate the working of java.util.IdentityHashMap.put() Method:
Program 1: When passing an existing key.




// Java code to illustrate the put() method
import java.util.*;
  
public class Identity_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty IdentityHashMap
        Map<Integer, String> identity_hash = new IdentityHashMap<Integer, String>();
  
        // Mapping string values to int keys
        identity_hash.put(10, "Geeks");
        identity_hash.put(15, "4");
        identity_hash.put(20, "Geeks");
        identity_hash.put(25, "Welcomes");
        identity_hash.put(30, "You");
  
        // Displaying the IdentityHashMap
        System.out.println("Initial Mappings are: " + identity_hash);
  
        // Inserting existing key along with new value
        String returned_value = (String)identity_hash.put(20, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);
  
        // Displayin the new map
        System.out.println("New map is: " + identity_hash);
    }
}


Output:

Initial Mappings are: {10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Returned value is: Geeks
New map is: {10=Geeks, 30=You, 20=All, 25=Welcomes, 15=4}

Program 2: When passing a new key.




// Java code to illustrate the put() method
import java.util.*;
  
public class Identity_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty IdentityHashMap
        Map<Integer, String> identity_hash = new IdentityHashMap<Integer, String>();
  
        // Mapping string values to int keys
        identity_hash.put(10, "Geeks");
        identity_hash.put(15, "4");
        identity_hash.put(20, "Geeks");
        identity_hash.put(25, "Welcomes");
        identity_hash.put(30, "You");
  
        // Displaying the IdentityHashMap
        System.out.println("Initial Mappings are: " + identity_hash);
  
        // Inserting existing key along with new value
        String returned_value = (String)identity_hash.put(50, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);
  
        // Displayin the new map
        System.out.println("New map is: " + identity_hash);
    }
}


Output:

Initial Mappings are: {10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Returned value is: null
New map is: {10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4, 50=All}

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments