TreeSet
is an ordered set, so any element you insert must implement Comparable
(unless you specify a custom Comparator
). Class
does not.
If you don't need the ordering, you can always use an unordered set such as HashSet. Otherwise, you'll need to come up with an ordering of your own.
From the Javadoc (emphasis mine):
A NavigableSet implementation based on
a TreeMap. The elements are ordered
using their natural ordering, or by a
Comparator provided at set creation
time, depending on which constructor
is used.
This implementation provides
guaranteed log(n) time cost for the
basic operations (add, remove and
contains).
Note that 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.
(See Comparable or Comparator 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 TreeSet
instance 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 set, equal.
The behavior of a set is well-defined
even if its ordering is inconsistent
with equals; it just fails to obey the
general contract of the Set interface.
See also: Comparator
cce.printStackTrace()
inside the catch block) – Mccomb