title: Using my own object as key in a TreeMap
body: <p>As TreeMap sorting is based only on the keys, I'm using a custom object as key in a treemap. I have respected in my opinion the contract between equals and compareTo in this case, if the two objects are equal, te compareTo returns 0.</p>
  
  <p>Below the code of the object:</p>
  
  <pre><code>public final class UserHighScore implements Comparable&lt;UserHighScore&gt;{
  
  private final int userId;
  private final int value;
  
  
  public UserHighScore(int userId, int value) {
      this.userId = userId;
      this.value = value;
  }
  
  public int getUserId() {
      return userId;
  }
  
  public int getValue() {
      return value;
  }
  
  
  @Override
  public boolean equals(Object obj) {
      if (obj == this) return true;
      if (!(obj instanceof UserHighScore)) {
          return false;
      }
      UserHighScore userHighScore = (UserHighScore) obj;
      return userHighScore.userId==userId;
  }
  
  
  @Override
  public int compareTo(UserHighScore uh) {
      if(uh.getUserId()==this.getUserId()) return 0;
      if(uh.getValue()&gt;this.getValue()) return 1;
      return -1;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And below the method that is causing the problem:</p>
  
  <p>If the user id's are the same I want to return 0 to avoid duplication, so then if I do map.put(userHighscore) it should automatically replace is there is another object in the map with the same userId.
  However if the users are diferent, I want them to be sorted according the their values.
  This approach is working perfectly fine for one thread, however my app is concurrent and when there are more than a threas, it is adding duplicates to the map. 
  My problem is with the highscores map, which is a concurrentHasmap and inside it contains a treemap.</p>
  
  <p>DO you see anything wrong with my approach?</p>