Don't understand why Set is not null

I suppose the answer is simple, but I see no explanation.

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

      

+3


source to share


2 answers


You have 1 set and 2 links per set ( a

and a link inside the card).

You are specifying one reference to null, but that does not mean that all other references will be set to zero.



Imagine that you are pointing to someone and I am pointing to the same person. Just because you stopped pointing does not mean that I will stop pointing.

Once the link inside the map is removed, the set is eligible for garbage collection.

+10


source


You are setting the local variable to null, not a reference inside the map.



If you want to set both values ​​to null, you simply remove Set from the Map.

0


source







All Articles