java map 克隆_HashMap对象的深层克隆

1.java.util.HashMap 的 clone 方法是浅层copy,clone出来的对象,仅仅是原来对象的一个引用,并且对克隆出来的对象进行操作是无效的。

下面是个例子:importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;/***@authorhzp

**/

public classTest {/***@paramargs*/

public static voidmain(String[] args) {//TODO Auto-generated method stub

HashMap source= newHashMap();

source.put("key1","value1");

source.put("key2","value2");for(Iterator keyItr =source.keySet().iterator();keyItr.hasNext();) {

Object key=keyItr.next();

System.out.println(key+ " : "+source.get(key));

}

System.out.println("----------------- 1 ----------------");

Map targetMap=(HashMap)source.clone();for(Iterator keyItr =targetMap.keySet().iterator();keyItr.hasNext();){

Object key=keyItr.next();

System.out.println(key+ " : "+source.get(key));

}

System.out.println("---------------- 2 ----------------");

Object temp= targetMap.put("key1","modify");

System.out.println("temp : "+temp);for(Iterator keyItr =source.keySet().iterator();keyItr.hasNext();){

Object key=keyItr.next();

System.out.println(key+ " : "+source.get(key));

}

}

}

输出结果为:

key1 : value1

key2 : value2----------------- 1 ----------------key1 : value1

key2 : value2---------------- 2 ----------------temp : value1

key1 : value1

key2 : value2

若想实现深层copy,则需要自己重写clone方法。

如下面的例子:importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;/***@authorhzp

**/

public classTest {class customHashMap extendsHashMap {publiccustomHashMap() {super();

}public customHashMap(intinitialCapacity) {super(initialCapacity);

}publicObject clone() {

Map target= newHashMap();for (Iterator keyIt = this.keySet().iterator(); keyIt.hasNext();) {

Object key=keyIt.next();

target.put(key,this.get(key));

}returntarget;

}

}/***@paramargs*/

public static voidmain(String[] args) {//TODO Auto-generated method stub

customHashMap source= (new Test()).newcustomHashMap();

source.put("key1", "value1");

source.put("key2", "value2");for (Iterator keyItr =source.keySet().iterator(); keyItr.hasNext();) {

Object key=keyItr.next();

System.out.println(key+ " : " +source.get(key));

}

System.out.println("----------------- 1 ----------------");

Map target=(Map) source.clone();

target.put("key1", "modify");

System.out.println("----------------- 2 the souce map print----------------");for (Iterator keyItr =source.keySet().iterator(); keyItr.hasNext();) {

Object key=keyItr.next();

System.out.println(key+ " : " +source.get(key));

}

System.out.println("----------------- 3 the target map print----------------");for (Iterator keyItr =target.keySet().iterator(); keyItr.hasNext();) {

Object key=keyItr.next();

System.out.println(key+ " : " +target.get(key));

}

}

}

输出结果:

key1 : value1

key2 : value2----------------- 1 ----------------

----------------- 2 the souce map ----------------key1 : value1

key2 : value2----------------- 3 the target map ----------------key1 : modify

key2 : value2


版权声明:本文为weixin_42392054原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。