Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TreeSet problem

0 views
Skip to first unread message

Steve Roy

unread,
Jun 12, 2001, 11:55:47 AM6/12/01
to
Hi,

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>


Steve Roy

unread,
Jun 12, 2001, 12:03:46 PM6/12/01
to
Steve Roy <sr...@mac.com> wrote:

> 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.

0 new messages