I recently read a book about java memory modelling, it says : HashMap use weak reference for keys and values(since they are all objects), so that hashmap can avoid out of memory issue when hasnmap stores more and more key value pairs.
But the problem is : if keys and values are being GC during the rumtime, how can I get the key value pair by using get method in the hashmap?
for example,
String key=new String("GC");
String value=new String("GC");
hashmap.put(key,value);
and after a certain execution of the code, it has a chance that java GC the key and value, then what happen during:
hashmap.get(key)
since the key is no longer exist in hashmap ?