HashMap對象的深層克隆

1.java.util.HashMap 的 clone 方法是淺層copy,clone出來的對象,僅僅是原來對象的一個引用,而且對克隆出來的對象進行操做是無效的。

下面是個例子:

 

    import java.util.HashMap;  
    import java.util.Iterator;  
    import java.util.Map;  
      
    /** 
     * @author hzp  
     * 
     */  
    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  

 

 

 

若想實現深層copy,則須要本身重寫clone方法。

 

以下面的例子:

    import java.util.HashMap;  
    import java.util.Iterator;  
    import java.util.Map;  
      
    /** 
     * @author hzp
     *  
     */  
    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  
相關文章
相關標籤/搜索