title: Accessing a HashSet using the HashCode directly? (Java)
body: <p>Hi I'm wondering if it is possible to access the contents of a HashSet directly if you have the Hashcode for the object you're looking for, sort of like using the HashCode as a key in a HashMap. </p>
  
  <p>I imagine it might work something sort of like this: </p>
  
  <pre><code>MyObject object1 = new MyObject(1); 
  
  Set&lt;MyObject&gt; MyHashSet = new HashSet&lt;MyObject&gt;();
  
  MyHashSet.add(object1)
  
  int hash = object1.getHashCode
  
  MyObject object2 = MyHashSet[hash]???
  </code></pre>
  
  <p>Thanks! </p>
  
  <p>edit: Thanks for the answers. Okay I understand that I might be pushing the contract of HashSet a bit, but for this particular project equality is solely determined by the hashcode and I know for sure that there will be only one object per hashcode/hashbucket. The reason I was pretty reluctant to use a HashMap is because I would need to convert the primitive ints I'm mapping with to Integer objects as a HashMap only takes in objects as keys, and I'm also worried that this might affect performance. Is there anything else I could do to implement something similar with? </p>