title: These Sets allow null. Why can't I add null elements?
body: <p>I want to know why HashSet, LinkedHashSet and TreeSet implementation does not allow null elements?
  Whenever i try to run the following code it throws a null pointer exception.</p>
  
  <pre><code>public static void main(String[] args) {
  
      HashSet&lt;Integer&gt; hashSet = new HashSet&lt;Integer&gt;();
  
      hashSet.add(2);
      hashSet.add(5);
      hashSet.add(1);
  //  hashSet.add(null);  will throw null pointer 
      hashSet.add(999);
      hashSet.add(10);
      hashSet.add(10);
      hashSet.add(11);
      hashSet.add(9);
      hashSet.add(10);
      hashSet.add(000);
      hashSet.add(999);
      hashSet.add(0);
  
      Iterator&lt;Integer&gt; it = hashSet.iterator();
      while(it.hasNext()){
          int i = it.next();
          System.out.print(i+" ");
      }
      }
  </code></pre>
  
  <p>Please guide me.</p>