title: How does HashMap in Java handle weak reference for keys and values?
body: <p>I recently read a book about java memory modelling, it says : <strong>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.</strong></p>
  
  <p>But the problem is : if keys and values are being GC during the rumtime, how can I get the key value pair by using <strong>get</strong> method in the hashmap?</p>
  
  <p>for example, </p>
  
  <pre><code>String key=new String("GC");
  String value=new String("GC");
  hashmap.put(key,value);
  </code></pre>
  
  <p>and after a certain execution of the code, it has a chance that java GC the key and value, then what happen during:</p>
  
  <pre><code>hashmap.get(key)
  </code></pre>
  
  <p>since the key is no longer exist in hashmap ?</p>