I'm using the TreeSet class from the 1.1 Collections classes, and I always
get an exception when adding a second object to the set.
import com.sun.java.util.collections.TreeSet;
public class TrivialApplication
{
public static void main(String[] args)
{
TreeSet set = new TreeSet();
String obj = "1";
set.add(obj);
System.out.println(obj + " added");
obj = "2";
set.add(obj);
System.out.println(obj + " added");
}
}
1 added
Exception Occurred:
java.lang.ClassCastException: java.lang.String
at com.sun.java.util.collections.TreeMap.compare(TreeMap.java:994)
at com.sun.java.util.collections.TreeMap.put(TreeMap.java:444)
at com.sun.java.util.collections.TreeSet.add(TreeSet.java:198)
at TrivialApplication.main(TrivialApplication.java)
I tried with other classes of objects, like Long and it also resulted in a
ClassCastException. What am I missing?
Steve
--
Steve Roy <sr...@mac.com>
> I'm using the TreeSet class from the 1.1 Collections classes, and I always
> get an exception when adding a second object to the set.
OK, I found out why. The Java 1.1 classes don't implement the Comparable
interface.