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?
Laythe Chamse
Greenhorn
Joined: Sep 06, 2009
Posts: 15
posted
0
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.
I implemented Comparable interface and it's compareTo() Method to MyOwnClass, the questions now is how should i edit the compareTo() Method to get the code to work?
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
Joined: Sep 06, 2009
Posts: 15
posted
0
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.
You can compare on two fields; first on one, and if that would lead to equality the other as well. Do a search around, that question has been asked before.
I managed to get the code to compile some how, but the problem now is that TreeSetX cant hold a String and Integer at the same time!
this is the Error i get:
Addind string object will give compareTo an idea onto how the sorting should be done. By adding the primitive integers, compareTo() has no way of sorting these values and throws exception at runtime.
Laythe Chamse
Greenhorn
Joined: Sep 06, 2009
Posts: 15
posted
0
The thing is that the homework i am doing ask me to add 3 Integer ojbects.
And this is the homework:
* Create your own NetBeans project named as MyTreeSet
* Add the following objects to the newly created MyTreeSet object
==> 2 String objects
==> 2 MyOwnClass object (You will have to create MyOwnClass.java first)
==> 3 Integer ojbects
* Create Iterator object from the MyTreeSet object and iterate them to get displayed.
Laythe Chamse
Greenhorn
Joined: Sep 06, 2009
Posts: 15
posted
0
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
Joined: Sep 08, 2009
Posts: 16
posted
0
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
Joined: Sep 06, 2009
Posts: 15
posted
0
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.
Laythe Chamse
Greenhorn
Joined: Sep 06, 2009
Posts: 15
posted
0
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.
Laythe Chamse
Greenhorn
Joined: Sep 06, 2009
Posts: 15
posted
0
Hello.
After doing a search on how to compare two fields, I tried this on my code but didnt work!
I extended Comparator interface dont worry.
So what should i do next?