關於Java中的HashMap的深淺拷貝的測試與幾點思考

來源:互聯網
上載者:User

標籤:dfa   賦值   pac   public   代碼   rri   else   eth   factor   

0、前言

工作忙起來後,許久不看演算法,竟然DFA敏感詞演算法都要看好一陣才能理解。。。真是和三階魔方還原手法一樣,田園將蕪,非常可惜啊。

在DFA演算法中,第一步是需要理解它的資料結構,在此基礎上,涉及到一些Hashmap的賦值。這裡的賦值非常有趣,三個Hashmap翻來覆去賦值,就解決了敏感詞表的初始化。

裡面都是屬於下文中的Hashmap“淺拷貝”,那麼究竟Java中的Hashmap有哪些拷貝方法呢?

1、測試代碼
HashMap hm_source = new HashMap();HashMap hm_clone = new HashMap();hm_source.put("1", "1");        // hashmap deep clone method 1hm_clone = (HashMap)hm_source.clone();// hashmap deep clone method 2hm_clone.putAll(hm_source);// hashmap shadow clone// hm_b = hm_a;hm_source.put("2", "2");System.out.println("hm_source增加元素後,hm_source:"+hm_source);System.out.println("hm_source增加元素後,hm_clone:"+hm_clone);System.out.println("是否指向同一記憶體位址:"+(hm_source==hm_clone));System.out.println("第一個元素是否指向同一記憶體位址:"+(hm_source.get(1)==hm_clone.get(1)));

上面介紹了兩種Hashmap深拷貝的方法,分別是hashmap.clone()和hashmap.putAll(hm_source),效果一樣,輸出如下:

hm_source增加元素後,hm_source:{1=1, 2=2}hm_source增加元素後,hm_clone:{1=1}是否指向同一記憶體位址:false第一個元素是否指向同一記憶體位址:true

那麼淺拷貝呢?(代碼中注釋的那段,直接等號=賦值),輸出如下:

hm_source增加元素後,hm_source:{1=1, 2=2}hm_source增加元素後,hm_clone:{1=1, 2=2}是否指向同一記憶體位址:true第一個元素是否指向同一記憶體位址:true
2、輸出解析

不難發現,深淺拷貝確實如其名,

深拷貝:兩個Hashmap對象似乎徹底無關,互相增加修改元素後,都不影響對方;

淺拷貝:兩個Hashmap對象就是“軟連結ln”,互相牽制,你改動了,我也跟著變。

3、上述猜想是否正確?

我黨的思想路線是實事求是,預想剖析根本區別,大家可以看看JDK clone函數的源碼。

但是從現象我們得到的初步結論有幾點:

  1. 淺拷貝的兩個對象使用的記憶體位址相同,深拷貝的對象地址“另立門戶”;
  2. 深拷貝的兩個對象也不完全“徹底無關”,僅僅是複製了元素的引用;

關於結論3.2,我也是在 HashMap的clone方法 博文中學習到了,裡面使用的Hashmap元素是Bean類型的,深拷貝下的元素修改,也會“打斷骨頭連著筋”地讓兩個Hashmap同時更新。

但是僅限於“元素修改”,如若“元素增刪”,那兩個Hashmap對象就“翻臉不認人”了,不會同步更新。

4、hashmap.clone()

JDK是個難得的學習材料,源碼還是要讀的。現在也粘貼過來,做一些記錄。

/**     * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and     * values themselves are not cloned.     * 【咱們中文叫“深拷貝”,老外美其名曰“拷貝一份執行個體的‘淺拷貝‘”,更加嚴謹】     * @return a shallow copy of this map     */    @SuppressWarnings("unchecked")    @Override    public Object clone() {        HashMap<K,V> result;        try {            result = (HashMap<K,V>)super.clone();        } catch (CloneNotSupportedException e) {            // this shouldn‘t happen, since we are Cloneable            throw new InternalError(e);        }        result.reinitialize();        result.putMapEntries(this, false);        return result;    }
/**     * Implements Map.putAll and Map constructor     *     * @param m the map     * @param evict false when initially constructing this map, else     * true (relayed to method afterNodeInsertion).     */    final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {        int s = m.size();        if (s > 0) {            if (table == null) { // pre-size                float ft = ((float)s / loadFactor) + 1.0F;                int t = ((ft < (float)MAXIMUM_CAPACITY) ?                         (int)ft : MAXIMUM_CAPACITY);                if (t > threshold)                    threshold = tableSizeFor(t);            }            else if (s > threshold)                resize();            for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {                K key = e.getKey();                V value = e.getValue();//【putVal方法裡面我初步掃了一下,也未涉及Hashmap instance對象的建立,是一些Hashmap結構中的Node的建立】                putVal(hash(key), key, value, false, evict);            }        }    }

以代碼最終解釋權由JDK1.8.x所有。

 

關於Java中的HashMap的深淺拷貝的測試與幾點思考

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.