用Java克隆HashMap
使用该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}热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短