title: HashMap with Null Key and Null Value
body: <p>Consider the following Code :</p>
  <pre><code>import java.util.*;
  
  class Employee {
      
      String name;
      
      public Employee(String nm) {
          this.name=nm;
      }
  }
  
  public class HashMapKeyNullValue {
      
      Employee e1;
      
      public void display(){
  
          Employee e2=null;
          Map map=new HashMap();
  
          map.put(e2, &quot;25&quot;);
          System.out.println(&quot;Getting the Value When e2 is set as KEY&quot;);
          System.out.println(&quot;e2 : &quot;+map.get(e2));
          System.out.println(&quot;e1 : &quot;+map.get(e1));
          System.out.println(&quot;null : &quot;+map.get(null));
  
          map.put(e1, &quot;&quot;);
          System.out.println(&quot;Getting the Value when e1 is set as KEY&quot;);
          System.out.println(&quot;e2 : &quot;+map.get(e2));
          System.out.println(&quot;e1 : &quot;+map.get(e1));
          System.out.println(&quot;null : &quot;+map.get(null));
  
          map.put(null, null);   // null as key and null as value
          System.out.println(&quot;Getting the Value when setting null as KEY and null as value&quot;);
          System.out.println(&quot;e2 : &quot;+map.get(e2));
          System.out.println(&quot;e1 : &quot;+map.get(e1));
          System.out.println(&quot;null : &quot;+map.get(null));
  
          map.put(null, &quot;30&quot;);
          System.out.println(&quot;Getting the Value when setting only null as KEY&quot;);
          System.out.println(&quot;e2 : &quot;+map.get(e2));
          System.out.println(&quot;e1 : &quot;+map.get(e1));
          System.out.println(&quot;null : &quot;+map.get(null));
      }
      
      public static void main(String[] args) {
          
          new HashMapKeyNullValue().display();
          
      }
  }
  </code></pre>
  <p>The Output of program is :</p>
  <pre><code>Getting the Value When e2 is set as KEY
  e2 : 25
  e1 : 25
  null : 25
  Getting the Value when e1 is set as KEY
  e2 : 
  e1 : 
  null : 
  Getting the Value when setting null as KEY and null as value
  e2 : null
  e1 : null
  null : null
  Getting the Value when setting only null as KEY
  e2 : 30
  e1 : 30
  null : 30
  </code></pre>
  <p>Here how <code>e1, e2, and null</code> as keys are related to each other. Are all three assigned to same hashcode? If yes, WHY?</p>
  <p>Since all three look different, the change in one value changes the other. Does it mean, only one entry for key is being made into <code>HashMap</code>  either <code>e1, e2 or null</code>? Because all are treated to be same key.</p>