• JAVAHashMap集合value存储list集合对象、类/包装类对象都是浅复制


    package reyo.sdk.utils.test;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    /**    
    * <B>创  建 人:</B>Administrator <BR>
    * <B>创建时间:</B>2022年3月23日 上午8:08:29<BR>
    * 
    * @author ReYo
    * @version 1.0
    */
    public class test5 {
    
    	/**    
    	 * @param args    
    	 */
    	public static void main(String[] args) {
    		List<String> strLists = new ArrayList<String>();//创建一个list集合对象
    		for (int i = 0; i < 10; i++) {
    			strLists.add("reyo-" + i);
    		}
    		String strObject = new String("reyo");
    		Map<String, Object> map = new HashMap<String, Object>();//创建一个hashmap集合对象
    		map.put("list", strLists);//list集合对象存入map的value中
    		map.put("name", strObject);//string对象存入map的value中
    		strLists.clear();//情况list集合数据
    		strObject = null;
    		for (Entry<String, Object> name : map.entrySet()) {
    			System.out.println(name);
    		}
    
    	}
    
    }
    

    运行该代码的结果如下:

     name=reyo
    list=[]
    现象:
         strLists集合对象存入到Map集合对象的value中,后续再清空strLists数据,发现map集合里的strLists数据也清空了
         strObject对象存入到map集合对象的value中,后续再清空strObject数据,返现map集合里的strObject数据没有清空

    查看hasmap的源码如下:

          Node<K,V>[] tab; Node<K,V> p; int n, i;
            if ((tab = table) == null || (n = tab.length) == 0)
                n = (tab = resize()).length;
            if ((p = tab[i = (n - 1) & hash]) == null)
                tab[i] = newNode(hash, key, value, null);//数据存储
            else {undefined
                Node<K,V> e; K k;
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                    e = p;
                else if (p instanceof TreeNode)
                    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
                else {undefined
                    for (int binCount = 0; ; ++binCount) {undefined
                        if ((e = p.next) == null) {undefined
                            p.next = newNode(hash, key, value, null);
                            if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                                treeifyBin(tab, hash);
                            break;
                        }
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            break;
                        p = e;
                    }
                }
                if (e != null) { // existing mapping for key
                    V oldValue = e.value;
                    if (!onlyIfAbsent || oldValue == null)
                        e.value = value;//新数据赋值
                    afterNodeAccess(e);
                    return oldValue;
                }
            }
         Node(int hash, K key, V value, Node<K,V> next) {undefined
                this.hash = hash;
                this.key = key;
                this.value = value;//value赋值
                this.next = next;
            }
    


    根据map集合里的实现看出可以简化如下代码再看:


    从如上图可以看出strLists集合赋值给另一个newStrListr集合,但是newStrListr集合没有创建新的内存空间。那就是strLists存储的数据块被newStrListr引用了。


    再看如下代码及执行情况:

    发现对象复制给另一个对象(没有创建新的存储空间)情况下,还是指向同一个存储空间。
    string=null或者name=null;实际上没有将存储空间数据回收掉,只是将指向存储空间的链路断开了。


  • 相关阅读:
    无语的Filezilla
    服务无法启动集中帖
    FileZilla Server-Can’t access file错误解决方法
    CCNA2.0笔记_OSPF v3
    CCNA2.0笔记_OSPF v2
    <转>32位移植到64位 注意事项
    异步设备IO 《windows核心编程》第10章学习
    关于自己五年来做出的选择分析
    2015过年之前计划
    关于同一线程两次调用EnterCriticalSection的测试
  • 原文地址:https://www.cnblogs.com/interdrp/p/16042446.html
Copyright © 2020-2023  润新知