title: How to check if object already exists in HashSet?
body: <p>This <a href="http://www.java2s.com/Tutorial/Java/0140__Collections/CheckifaparticularelementexistsinHashSet.htm" rel="nofollow noreferrer">example</a> makes it look easy:</p>
  
  <pre><code>HashSet&lt;Integer&gt; hSet = new HashSet&lt;Integer&gt;();
  
  hSet.add(new Integer("1"));
  hSet.add(new Integer("2"));
  hSet.add(new Integer("3"));
  
  System.out.println(hSet.contains(new Integer("3")));
  </code></pre>
  
  <blockquote>
    <p>true</p>
  </blockquote>
  
  <p>But when I use it on a Movie object (which I want to check):</p>
  
  <pre><code>Set&lt;Movie&gt; hSet = new HashSet&lt;Movie&gt;();
  
  hSet.add(new Movie(222, "Lord of the Rings", "something"));
  System.out.println(hSet);
  
  System.out.println(hSet.contains(new Movie(222, "Lord of the Rings", "something")));
  </code></pre>
  
  <blockquote>
    <p>false</p>
  </blockquote>
  
  <p>The hSet print shows:</p>
  
  <blockquote>
    <p>[id: 222name: Lord of the Rings]</p>
  </blockquote>
  
  <p>I don't see a difference in adding a new Integer or adding a new movie object to the list. So why isn't my example working?</p>
  
  <p>//edit.</p>
  
  <p>If anyone is looking for a good reference, <a href="https://stackoverflow.com/questions/15722485/hashset-storing-equal-objects">this helped me out</a>.</p>