title: Java HashSet contains Object
body: <p>I made my own class with an overridden equals method which just checks, if the names (attributes in the class) are equal. Now I store some instances of that class in a HashSet so that there are no instances with the same names in the HashSet.</p>
  
  <p>My Question: How is it possible to check if the HashSet contains such an object. .contains() wont work in that case, because it works with the .equals() method. I want to check if it is really the same object.</p>
  
  <p>edit:</p>
  
  <pre><code>package testprogram;
  
  import java.util.HashSet;
  import java.util.Set;
  
  public class Example {
      private static final Set&lt;Example&gt; set = new HashSet&lt;Example&gt;();
      private final String name;
      private int example;
  
      public Example(String name, int example) {
          this.name = name;
          this.example = example;
          set.add(this);
      }
  
      public boolean isThisInList() {
          return set.contains(this);
          //will return true if this is just equal to any instance in the list
          //but it should not
          //it should return true if the object is really in the list
      }
  
      public boolean remove() {
          return set.remove(this);
      }
  
      //Override equals and hashCode
  }
  </code></pre>
  
  <p>Sorry, my english skills are not very well. Please feel free to ask again if you don't understand what I mean.</p>