title: Why the Java TreeSet reject adding the elements with the same comparing key instead of equals key?
body: <p>I'm using TreeSet to save unique employees and sort by name. But I got strange results.
  Here is the code:</p>
  
  <pre><code>
  import java.util.Comparator;
  import java.util.Objects;
  import java.util.SortedSet;
  import java.util.TreeSet;
  
  class Employee implements Comparable&lt;Employee&gt; {
      private int id;
      private String name;
  
      public Employee(int id, String name) {
          this.id = id;
          this.name = name;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      // Two Employees are equal if their IDs are equal
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
          Employee employee = (Employee) o;
          return id == employee.id;
      }
  
      @Override
      public int hashCode() {
          return Objects.hash(id);
      }
  
      // Compare employees based on their IDs
      @Override
      public int compareTo(Employee employee) {
          return this.getId() - employee.getId();
      }
  
      @Override
      public String toString() {
          return "Employee{" +
                  "id=" + id +
                  ", name='" + name + '\'' +
                  '}';
      }
  
      public static void main(String[] args) {
          SortedSet&lt;Employee&gt; employees = new TreeSet&lt;&gt;();
  
          // Providing a Custom Comparator (This comparator compares the employees based on their Name)
          employees = new TreeSet&lt;&gt;(Comparator.comparing(Employee::getName));
  
          employees.add(new Employee(1010, "Rajeev"));
          employees.add(new Employee(1011, "Rajeev"));
          employees.add(new Employee(1005, "Sachin"));
          employees.add(new Employee(1008, "Chris"));
  
          System.out.println("\nEmployees (sorted based on the supplied Comparator)");
          System.out.println(employees);
      }
  }
  
  </code></pre>
  
  <p>Here is the output:</p>
  
  <pre><code>Employees (sorted based on the supplied Comparator)
  [Employee{id=1008, name='Chris'}, Employee{id=1010, name='Rajeev'}, Employee{id=1005, name='Sachin'}]
  
  </code></pre>
  
  <p>I'm using openJDK-11.0.1_1_macosx. The object employees.add(new Employee(1011, "Rajeev")); was rejected. I thought it rejects the object with the duplicated employ.id but not the employ.name.</p>
  
  <p>Thanks for your help.</p>