ClassCastException when adding two different types to TreeSet
The following code throws a ClassCastException:
TreeSet<Identity> members = new TreeSet<Identity>();
Star star = new Star();
members.add(star);
Jovian jov = new Jovian();
members.add(jov);
Both Jovian and Star inherit from CelestialBody, which, in turn, inherits from Identity.
why the ClassCastException?
> why the ClassCastException? What does the compiler say?Persumably it identifies some line - either of the code you posted or as part of what goes on in the various Comparators/Comparables involved.
try to replace your TreeSet with a HashSet, your code will compile and run in a good way.This is because TreeSet is naturally sorted Collection then its elements have to be mutually comparable.Good Luck.Ahmad Elsafty
sorry the ClassCastException is on the last line.
BTW ive tried:
TreeSet members = new TreeSet();
Star star = new Star();
members.add(star);
Jovian jov = new Jovian();
members.add(jov);
with the same result, and doing this:
TreeSet<? extends Identity> members = new TreeSet<Identity>();
Star star = new Star();
members.add(star);
Jovian jov = new Jovian();
members.add(jov);
results in compile time errors at lines 3 and 5.
-sigh-
ahh yes, but 1. the sorting is essential and 2. Star and Jovian are mutually comparable
here's the trace (not that it really illuminates anything):
java.lang.ClassCastException: com.zenithhouse.celestial.Jovian
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)
at com.zenithhouse.celestial.foundry.RegularJovianDiscFactoryUnitTest.testAddingMember(RegularJovianDiscFactoryUnitTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
You objects need to implement the Comparable interface.See here: http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
1. Identity implements Comparator2. CelestialBody extends Identity3. Star extends CelestialBody4. Jovian extends CelestialBodytherefore Star and Jovian implement Comparator, right?
actually, I've been reading the javadoc on Comparable and Comparator, and they both kinda look the same to me. What's the difference?
> 1. Identity implements ComparatorYou mean Comparable, right? How does Identity implement it? Can it throw a ClassCastException?
Simple... Different authors at different times for different jobs.
You need to implement Comparable... which underlies all sort of sorting in the Collections framework.
A Comparator is an object which allows other objects to be compared.
A Comparable object is one which can be compared "inherently".
it also appears that the exception seems to be coming from your compare method, so you should look there.
aha!thank you corlettk. That was the problem! changed to comaprable and all is well with the world again.thanks to all
> actually, I've been reading the javadoc on Comparable
> and Comparator, and they both kinda look the same to
> me. What's the difference?
Implementing Comparable means that the objects of a class can be compared to each other, that they have some natural order. For example strings have a can be ordered lexicographically so that a < aa < ab < b < ba.
Comparators are for implementing an order different from the natural. You might for example want to reverse the natural order or base the comparison on some other characteristic. For example there are comparators that enforce a national standard ordering. For example in English you have a < ?< z while in Finnish and Swedish you have a < z < ?