I create a WeakHashMap as
WeakHashMap<Employee,String> map = new WeakHashMap<Employee,String>();
map.put(emp,"hello");
where emp is an Employee object. Now if I do emp = null or say emp object is no longer referenced, then will the entry be removed from the WeakHashMap i.e. will the size of Map be zero?
And will it be vice-versa in case of HashMap?
Is my understanding of WeakHashMap correct?
I ran the sample code to understand the difference between
HashMap
andWeakHashMap
The output will be: