title: Should a HashSet be allowed to be added to itself in Java?
body: <p>According to the contract for a Set in Java, "it is not permissible for a set to contain itself as an element" (<a href="https://docs.oracle.com/javase/8/docs/api/java/util/Set.html" rel="noreferrer">source</a>). However, this is possible in the case of a HashSet of Objects, as demonstrated here:</p>
  
  <pre><code>Set&lt;Object&gt; mySet = new HashSet&lt;&gt;();
  mySet.add(mySet);
  assertThat(mySet.size(), equalTo(1));
  </code></pre>
  
  <p>This assertion passes, but I would expect the behavior to be to either have the resulting set be 0 or to throw an Exception. I realize the underlying implementation of a HashSet is a HashMap, but it seems like there should be an equality check before adding an element to avoid violating that contract, no? </p>