The constructor you used for TreeSet is new TreeSet<String>(comp), in Java API it says:
public TreeSet(Comparator<? super E> comparator)
Constructs a new, empty tree set, sorted according to the specified comparator.
And for the Comparator:
The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.
Since TreeSet implements SortSet, and in API SortSet notes:
Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be consistent with equals if the sorted set is to correctly implement the Set interface. (See the Comparable interface or Comparator interface for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a sorted set performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the sorted set, equal. The behavior of a sorted set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
When trying to add "Third" to the TreeSet, it compares with existing element "First" and returns 0 and won't be able to sort the set, so it failed.
I have a question though, the TreeSet's add method says:
public boolean add(E e)
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.
It does not mention sort or compare stuff, so it seems as long as the new element fulfil the condition (e==null ? e2==null : e.equals(e2)), it should be able to be added. It seems contradictory on this example?
This thread is a bit old, but there's not really a good answer, so I'll post this.
The previous post is correct in stating that if 2 items, when compared in a TreeSet, return 0, the the item doesn't get added. So, if they are equal, I ran the regular String compare to order alphabetically.
Please remember that you have to tailor an exam answer to the version of Java® current at the time; ten years ago we used Java6. Nowadays most people would write a λ maybe using some of the methods introduced in 2014 (Java8). I would do something like this:-Jia Tan has told us why that Comparator is permissible only in a cert exam revision book. Did anybody notice that return 1/-1; is wrong. That is why you are getting strange behaviour wherebydoesn't baheve the same as...and the reason the TreeSet#add() method doesn't say anything about consistency with equals() is that the top of the documentation page says this:-
the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface.