java.util.IdentityHashMap.put() Method


The put(K key, V value) method is used to associate the specified value with the specified key in this identity hash map. If the map previously contained a mapping for this key, the old value is replaced.


Following is the declaration for java.util.IdentityHashMap.put() method.

public V put(K key, V value)


  • key − This is the key with which the specified value is to be associated.

  • value − This is the value to be associated with the specified key.

Return Value

The method call returns the previous value associated with key, or null if there was no mapping for key.




The following example shows the usage of java.util.IdentityHashMap.put()

package com.tutorialspoint;

import java.util.*;

public class IdentityHashMapDemo {
   public static void main(String args[]) {

      // create identity hash map
      IdentityHashMap ihmap = new IdentityHashMap();

      // populate the map
      ihmap.put(1, "java");
      ihmap.put(2, "util");
      ihmap.put(3, "package");

      System.out.println("Initial map: " + ihmap);

      // put value at key 2
      ihmap.put(2, "awt");

      System.out.println("Map after modification: " + ihmap);

Let us compile and run the above program, this will produce the following result.

Initial map: {2=util, 3=package, 1=java}
Map after modification: {2=awt, 3=package, 1=java}
Kickstart Your Career

Get certified by completing the course

Get Started