Clone IdentityHashMap in Java



To clone the IdentityHashMap in Java, use the clone() method.

Create an IdentityHashMap and add elements to it −

IdentityHashMap<String, Integer> m = new IdentityHashMap<String, Integer>();
m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);

Now get the size of the IdentityHashMap −

m.size()

The following is an example to clone IdentityHashMap in Java −

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      IdentityHashMap<String, Integer> m = new IdentityHashMap<String, Integer>();
      m.put("1", 100);
      m.put("2", 200);
      m.put("3", 300);
      m.put("4", 150);
      m.put("5", 110);
      m.put("6", 50);
      m.put("7", 90);
      m.put("8", 250);
      m.put("9", 350);
      m.put("10", 450);
      System.out.println("IdentityHashMap elements\n"+ m);
      System.out.println("Size = " + m.size());
      System.out.println("\nCloned Map = " + m.clone());
      System.out.println("Size = " + m.size());
   }
}

Output

IdentityHashMap elements
{2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Size = 10

Cloned Map = {2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Size = 10
Updated on: 2019-07-30T22:30:24+05:30

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements