title: Comparing objects in HashSet
body: <p>I'm making a 2D game that has a stars in it. I decided to create constructor in class named Star that gives random coordinates.</p>
  
  <pre><code>public Star(){
      super(0,0);
      x = randomX.nextInt(maxX - minX + 1);
      y = randomY.nextInt(maxX - minY + 1);
  }
  </code></pre>
  
  <p>Then, in other class I put them in HashSet</p>
  
  <pre><code>Set&lt;Star&gt; star = new HashSet&lt;&gt;();
  
  public Set&lt;Star&gt; generateStars(){
      while (star.size() &lt; numberOfStars){
              star.add(new Star());
      }
      return star;
  }
  </code></pre>
  
  <p>Of course, I have render and tick methods but I think it's not worth to paste them. My lecturer told me that there can be same stars and to prevent that I should use identity function using hashcodes. Can someone help me figure that out ? I imagine that this function should check if the hashcodes are the same and if it's the case it should return only one value that way we will add 1 object instead of 2 into the HashSet. Am I right ?</p>