JAVA-HashMap集合value存储list集合对象、类/包装类对象--都是浅复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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的源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;实际上没有将存储空间数据回收掉,只是将指向存储空间的链路断开了。


posted @   锐洋智能  阅读(398)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· Windows 提权-UAC 绕过
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2010-03-23 酒店管理系统web版/Java酒店管理系统:功能详解(开房操作)
点击右上角即可分享
微信分享提示