title: Why can't custom comparator be used with hashSet to check duplicate objects
body: <p>I want to use a custom comparator to filter duplicate objects from a <code>HashSet</code>.</p>
  <p>In the <code>Mobile</code> class, I have defined a non-static nested <code>Comparator</code> class for comparing. The criterium which is used by this comparator is the <code>id</code> field of the <code>Mobile</code> object.</p>
  <pre><code>Set&lt;Mobile&gt; treeSet = new TreeSet&lt;&gt;(new Mobile().new Comparator()); // works
  
  
  Set&lt;Mobile&gt; hashSet = new HashSet&lt;&gt;(new Mobile().new Comparator()); // does not work
  </code></pre>
  <p>Even though it works perfectly for a <code>TreeSet</code>, Eclipse shows a syntax error when I try this with a <code>HashSet</code>,</p>
  <p>It appears that I need to override the <code>equals</code> and <code>hashcode</code> methods in the <code>Mobile</code> class if I want to use a <code>HashSet</code>. However, I would prefer to use a comparator instead.</p>
  <p>Why is this the case?</p>