title: Why does TreeSet throw a ClassCastException?
body: <p>I am trying to add two 'Employee' objects to a TreeSet:</p>
  
  <pre><code>Set&lt;Employee&gt; s = new TreeSet&lt;Employee&gt;();
  s.add(new Employee(1001));
  s.add(new Employee(1002));
  </code></pre>
  
  <p>But it throws a ClassCastException:</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable
      at java.util.TreeMap.put(TreeMap.java:542)
      at java.util.TreeSet.add(TreeSet.java:238)
      at MyClient.main(MyClient.java:9)
  </code></pre>
  
  <p>But if I add only one object to the TreeSet:</p>
  
  <pre><code>Set&lt;Employee&gt; s = new TreeSet&lt;Employee&gt;();
  s.add(new Employee(1001));
  </code></pre>
  
  <p>Or if I use a HashSet instead:</p>
  
  <pre><code>Set&lt;Employee&gt; s = new HashSet&lt;Employee&gt;();
  s.add(new Employee(1001));
  s.add(new Employee(1002));
  </code></pre>
  
  <p>Then it is successful. Why does the exception happen and how do I fix it?</p>