Java Collection Tutorial - Java IdentityHashMap.clone()


IdentityHashMap.clone() has the following syntax.

public Object clone()


In the following code shows how to use IdentityHashMap.clone() method.

/*  w ww. j  a v  a 2 s.  c o m*/
import java.util.IdentityHashMap;
import java.util.Map;

public class Main {
  public static void main(String[] argv) throws Exception {
    IdentityHashMap<Object, Object> objMap = new IdentityHashMap<Object, Object>();

    Object o1 = new Integer(123);
    Object o2 = new Integer(123);
    objMap.put(o1, "first");
    objMap.put(o2, "from");

    Object v1 = objMap.get(o1); 
    Object v2 = objMap.get(o2); 

      // clone the map
    IdentityHashMap ihmapclone=(IdentityHashMap)objMap.clone();

    System.out.println("Cloned map content: " + ihmapclone);


The code above generates the following result.