I am trying to add two \'Employee\' objects to a TreeSet:
Set s = new TreeSet();
s.add(new Employee(1001));
s.add(new Employe
TreeSet requires elements to implement the Comparable interface if a custom Comparator is not set. HashSet uses the equals/hashCode contract instead.
You can add only one element into TreeSet which does not implement Comparable because it does not need to be compared with other elements.
Take a look at the TreeMap.put(K key, V value) source code and you'll clearly see the reasons behind all your questions (TreeSet is based on TreeMap, hence the source reference).