使用该clone()方法克隆HashMap。
以下是我们的带有元素的HashMap-
HashMap hm1 = new HashMap();
hm1.put("Shirts", new Integer(700));
hm1.put("Trousers", new Integer(600));
hm1.put("Jeans", new Integer(1200));
hm1.put("Android TV", new Integer(450));
hm1.put("Air Purifiers", new Integer(300));
hm1.put("Food Processors", new Integer(950));创建另一个HashMap并将第一个HashMap克隆到其中-
HashMap hm2 = (HashMap)hm1.clone();
以下是在Java中克隆HashMap的示例-
import java.util.*;
public class Demo {
   public static void main(String args[]) {
      //创建哈希映射
      HashMap hm1 = new HashMap();
      hm1.put("Shirts", new Integer(700));
      hm1.put("Trousers", new Integer(600));
      hm1.put("Jeans", new Integer(1200));
      hm1.put("Android TV", new Integer(450));
      hm1.put("Air Purifiers", new Integer(300));
      hm1.put("Food Processors", new Integer(950));
      System.out.println("Map 1 = "+hm1);
      HashMap hm2 = (HashMap)hm1.clone();
      System.out.println("Cloned Map = "+hm2);
   }
}输出结果
Map 1 = {Backpack=1200, Belt=600, Wallet=700}
Cloned Map = {Backpack=1200, Belt=600, Wallet=700}