我想答案很简单,但我没有看到解释.
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
Run Code Online (Sandbox Code Playgroud)
Kay*_*man 10
你有1套和2套参考(a和地图内的参考).
您将一个引用设置为null,但这并不意味着所有其他引用都将设置为null.
想象一下,你指着某人,我指着同一个人.仅仅因为你停止指点,并不意味着我会停止指点.
删除映射中的引用后,该集合符合垃圾回收的条件.