Java IdentityHashMap - put() Method
The java.util.IdentityHashMap.put() method is used to map the specified key to the specified value in the identity hash map. If the key is already present in the map, the old value is replaced.
Syntax
public V put(K key, V value)
Here, K and V are the type of key and value respectively maintained by the container.
Parameters
key |
Specify the key with which the specified value is to be associated. |
value |
Specify the value to be associated with the specified key. |
Return Value
Returns the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)
Exception
NA
Example:
In the example below, the java.util.IdentityHashMap.put() method is used to map the specified key to the specified value in the given identity hash map.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a identity hash map IdentityHashMap<Integer, String> MyMap = new IdentityHashMap<Integer, String>(); //populating the map MyMap.put(101, "John"); MyMap.put(102, "Marry"); MyMap.put(103, "Kim"); MyMap.put(104, "Jo"); //printing the map System.out.println("MyMap contains: " + MyMap); //change a key-value pair MyMap.put(103, "Ramesh"); //printing the map System.out.println("MyMap contains: " + MyMap); } }
The output of the above code will be:
MyMap contains: {102=Marry, 104=Jo, 103=Kim, 101=John} MyMap contains: {102=Marry, 104=Jo, 103=Ramesh, 101=John}
❮ Java.util - IdentityHashMap