title: How to override hashcode and equals method to avoid adding duplicate strings in HashSet in java?
body: <p>In my code I have a set of PlacesInfo object ie., </p>
  
  <pre><code> Set&lt;PlacesInfo&gt; placeId;    
  </code></pre>
  
  <p>In this set I am adding placeId (String). I need to avoid adding duplicates to my HashSet. Here is my override method below. but, still it is adding duplicate elements to my set. So, how to avoid this?</p>
  
  <pre><code>@Override
  public int hashCode() {
      int hash = 5;
      hash = 97 * hash + Objects.hashCode(this.placeId);
      return hash;
  }
  
  @Override
  public boolean equals(Object obj) {
      if (obj == null) {
          return true;
      }
      if (this.getClass() != obj.getClass()) {
          return false;
      }
      final PlacesInfo other = (PlacesInfo) obj;
      if (!Objects.equals(this.placeId, other.placeId)) {
          return false;
      }
      return true;
  }
  </code></pre>