why do i get TreeSet RuntimeException with my code?

Laythe Chamse

Greenhorn

Posts: 15

posted 8 years ago

Hello EveryOne At JavaRanch I have a problem with my code, why do i get this Error:

Exception in thread "main" java.lang.ClassCastException: MyOwnClass cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
at java.util.TreeSet.add(TreeSet.java:238)
at MyTreeSet.main(MyTreeSet.java:21)

TreeSet sorts its entries by their "natural order". In order to be able to determine that order, the entries must implement the Comparable interface so that that order can be determined. Your class does not.

Think about it for a moment. Given the definition of your class, how would TreeSet know how to sort instances of it?

Bear Bibeault wrote:TreeSet sorts its entries by their "natural order". In order to be able to determine that order, the entries must implement the Comparable interface so that that order can be determined. Your class does not.

Think about it for a moment. Given the definition of your class, how would TreeSet know how to sort instances of it?

I gived it some thoughts but i dont get it well should i extends the Comparable interface?, and should i compare my TreeSetX to what specificaly?
can you give me an example Please.

The compile error message says it all : MyOwnClass cannot be cast to java.lang.Comparable.
As Bear said, TreeSet automatically sorts its entries by casting its entries to Comparable and calling its compareTo method. MyOwnClass doesn't implement Comparable, so the TreeSet fails to cast it to Comparable. MyOwnClass must implement Comparable.

Depends--how do you want MyOwnClass to be compared to other instances? I'd assume you'd just compare the string properties, but I'm just guessing.

Laythe Chamse

Greenhorn

Posts: 15

posted 8 years ago

David Newton wrote:Depends--how do you want MyOwnClass to be compared to other instances? I'd assume you'd just compare the string properties, but I'm just guessing.

In the cod i am working on i would like MyOwnClass to be compared to the string properties so how can i do it?
and what about if i wanted MyOwnClass to be compared to String and Integer at the same time is that possible?

Laythe Chamse wrote:.... and what about if i wanted MyOwnClass to be compared to String and Integer at the same time is that possible?

With Comparable Not possible, you can sort the objects in the (only) way defined in compareTo() method. Or use a Comparator and pass that to the constructor if you want objects sorted in different ways in different places.

* Create Iterator object from the MyTreeSet object and iterate them to get displayed.

Laythe Chamse

Greenhorn

Posts: 15

posted 8 years ago

So AnyOne can help me please.
Remeber i already 99% done with the homework so you are not helping me cheating or something beside it's just a homework for SelfLearning.

Jaydeep Vaishnav

Greenhorn

Posts: 16

posted 8 years ago

It seems that you are supposed to work with HashSet and not TreeSet. There is no requirement of sorting as well. Seems to me a straightforward question. Your code needs minor changes. Just to give you a hint that think in terms of adding an object and you are good.

Laythe Chamse

Greenhorn

Posts: 15

posted 8 years ago

Jaydeep Vaishnav wrote:It seems that you are supposed to work with HashSet and not TreeSet. There is no requirement of sorting as well. Seems to me a straightforward question. Your code needs minor changes. Just to give you a hint that think in terms of adding an object and you are good.

Actualy i am studing the java colection, i am done with the HashSet and i had a homework just like this one in HashSet as well and i have done it, now i should do the homework practicing TreeSet not HashSet i am possitively sure about it, unless the one that puted the homework is mistaken somewhere but i doubte it.

Note: i have mistaked copied the HashSet homework but i corected it and it's the same just HashSet is changed with TreeSet if that what you are mentioning.

A TreeSet isn't meant to hold different kinds of objects, because all these objects need to be compared to each other. If your teacher is asking you to compare Strings to Integers, he's basically asking you to compare apples and oranges. With a custom Comparator you can still do it, but it's going to be harder.

Rob Prime wrote:A TreeSet isn't meant to hold different kinds of objects, because all these objects need to be compared to each other. If your teacher is asking you to compare Strings to Integers, he's basically asking you to compare apples and oranges. With a custom Comparator you can still do it, but it's going to be harder.

Thank you Sir Rob Prime now i understand where the problem is.
So can you point me to the right direction to custon a Comparator to solve the problem, I love challenges so would you give me a few Hints Pealse

I'd go the easy way: use a ToStringComparator:
That will not allow you to add both "1" and 1 though. Another solution is compare on class names first, toString() values later. Do a search to see how to compare on two fields.