Java IdentityHashMap clone() Method



Description

The Java IdentityHashMap clone() method is used to return a shallow copy of this identity hash map.

Declaration

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

public Object clone()

Parameters

NA

Return Value

The method call returns a shallow copy of this map.

Exception

NA

Getting Clone of an IdentityHashMap of Integer, Integer Pair Example

The following example shows the usage of Java IdentityHashMap clone() method to get a shallow copy of a Map. We've created two Map objects of Integer,Integer pairs. Then few entries are added to one map, another map is populated using clone() method and then both maps are printed.

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      IdentityHashMap<Integer, Integer> newmap1 = new IdentityHashMap<>();
      IdentityHashMap<Integer, Integer> newmap2 = new IdentityHashMap<>();

      // populate 1st map
      newmap1.put(1, 1);
      newmap1.put(2, 1);
      newmap1.put(3, 1); 

      // clone 1st map
      newmap2 = (IdentityHashMap)newmap1.clone();

      System.out.println("1st Map: " + newmap1);
      System.out.println("Cloned 2nd Map: " + newmap2);   
   }    
}

Output

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

1st Map: {2=1, 3=1, 1=1}
Cloned 2nd Map: {2=1, 3=1, 1=1}

Getting Clone of an IdentityHashMap of Integer, String Pair Example

The following example shows the usage of Java IdentityHashMap clone() method to get a shallow copy of a Map. We've created two Map objects of Integer,String pairs. Then few entries are added to one map, another map is populated using clone() method and then both maps are printed.

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      IdentityHashMap<Integer, String> newmap1 = new IdentityHashMap<>();
      IdentityHashMap<Integer, String> newmap2 = new IdentityHashMap<>();

      // populate 1st map
      newmap1.put(1, "A");
      newmap1.put(2, "B");
      newmap1.put(3, "C"); 

      // clone 1st map
      newmap2 = (IdentityHashMap)newmap1.clone();

      System.out.println("1st Map: " + newmap1);
      System.out.println("Cloned 2nd Map: " + newmap2);   
   }    
}

Output

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

1st Map: {2=B, 3=C, 1=A}
Cloned 2nd Map: {2=B, 3=C, 1=A}

Getting Clone of an IdentityHashMap of Integer, Object Pair Example

The following example shows the usage of Java IdentityHashMap clone() method to get a shallow copy of a Map. We've created two Map objects of Integer,Student pairs. Then few entries are added to one map, another map is populated using clone() method and then both maps are printed.

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      IdentityHashMap<Integer, Student> newmap1 = new IdentityHashMap<>();
      IdentityHashMap<Integer, Student> newmap2 = new IdentityHashMap<>();

      // populate hash map
      newmap1.put(1, new Student(1, "Julie"));
      newmap1.put(2, new Student(2, "Robert"));
      newmap1.put(3, new Student(3, "Adam"));

      // clone 1st map
      newmap2 = (IdentityHashMap)newmap1.clone();

      System.out.println("1st Map: " + newmap1);
      System.out.println("Cloned 2nd Map: " + newmap2);   
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

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

1st Map: {1=[ 1, Julie ], 3=[ 3, Adam ], 2=[ 2, Robert ]}
Cloned 2nd Map: {1=[ 1, Julie ], 3=[ 3, Adam ], 2=[ 2, Robert ]}
java_util_identityhashmap.htm
Advertisements