java.util.HashMap的clone方法

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

下面是个例子:


 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author wxb 
 *
 */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        HashMap source = new HashMap();
        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

 

 

 

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

 

如下面的例子:

 

 

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author wxb
 * 
 */
public class Test {

	class customHashMap extends HashMap {

		public customHashMap() {
			super();
		}

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

		public Object clone() {
			Map target = new HashMap();
			for (Iterator keyIt = this.keySet().iterator(); keyIt.hasNext();) {
				Object key = keyIt.next();
				target.put(key, this.get(key));
			}
			return target;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		customHashMap source = (new Test()).new customHashMap();
		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

 

 

资料:

 

http://www.gznc.edu.cn/yxsz/jjglxy/book/Java_api/java/util/HashMap.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值