title: Why does not Java HashSet.equals() check for object equality?
body: <p>I've come to this phenomenon this morning, the <code>equals</code> method in <code>Set</code> does not check for value equality of the element while <code>List</code> does. This is not in accordance with the java doc.</p>
  
  <pre><code>Set&lt;MyClass&gt; s1 = new HashSet&lt;&gt;();
  Set&lt;MyClass&gt; s2 = new HashSet&lt;&gt;();
  Set&lt;MyClass&gt; s3 = new HashSet&lt;&gt;();
  Set&lt;MyClass&gt; s4 = new HashSet&lt;&gt;();
  List&lt;MyClass&gt; l1 = new ArrayList&lt;&gt;();
  List&lt;MyClass&gt; l2 = new ArrayList&lt;&gt;();
  
  MyClass o1 = new MyClass();
  MyClass o2 = new MyClass();
  
  // **this gives false, and does not call MyClass.equals().**
  s1.add(o1);
  s2.add(o2);
  boolean setCompareWithDifferentObjects = s1.equals(s2);
  
  // this gives true, and also does not call MyClass.equals().
  s3.add(o1);
  s4.add(o1);
  boolean setCompareWithSaveObjects = s3.equals(s4);
  
  // this give true, and MyClass.equals() is called.
  l1.add(o1);
  l2.add(o2);
  boolean listCompare = l1.equals(l2)
  </code></pre>
  
  <p>I've done some research. According to this 
  <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Set.html#equals(java.lang.Object)" rel="nofollow noreferrer">Java doc for Set</a>, <a href="https://docs.oracle.com/javase/7/docs/api/java/util/AbstractSet.html#equals(java.lang.Object)" rel="nofollow noreferrer">HashSet equals</a> , <a href="https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#containsAll(java.util.Collection)" rel="nofollow noreferrer">HashSet containsAll</a>, <a href="https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#contains(java.lang.Object)" rel="nofollow noreferrer">HashSet contains</a>, it will use (o==null ? e==null : o.equals(e)) to check whether the elements are equal. So why is this happen? Can anyone give me some hint on this?</p>
  
  <p>Thanks!</p>
  
  <p>----------Answer to this question can be found here -----------</p>
  
  <p><a href="https://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java">What issues should be considered when overriding equals and hashCode in Java?</a></p>
  
  <p>I overrided equals() but not hashCode()...</p>
  
  <p>btw, the same set comparison worked in groovy, even if hashCode() is not overriden.</p>