簡體   English   中英

不明白為什么Set不為null

[英]Don't understand why Set is not null

我想答案很簡單,但我沒有看到解釋。

Map<String, Set<String>> m = new HashMap<String, Set<String>>();
Set<String> a = new HashSet<String>();

a.add("a");
a.add("b");
a.add("c");

m.put("set", a); // reference    
a = null; // if I type a.remove("b"); variable m holds only a and c as it should

System.out.println(m.get("set")); // Why this prints [a, b, c] as it should null or empty

你有1套和2套參考集( a和地圖內的參考)。

您將一個引用設置為null,但這並不意味着所有其他引用都將設置為null。

想象一下,你指着某人,我指着同一個人。 僅僅因為你停止指點,並不意味着我會停止指點。

刪除映射中的引用后,該集合符合垃圾回收的條件。

您將局部變量設置為null而不是Map中的引用。

如果要將兩者都設置為null,則可以從地圖中刪除Set。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM