Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why do i get TreeSet RuntimeException with my code?

 
Laythe Chamse
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

This is my code:

First Class:


Second Class:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Laythe Laythe", please check your private messages for an important administrative matter.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Laythe Chamse
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thank you all for your help.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Laythe Chamse
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

Can i make TreeSetX to hold a String and Integer at the same time?

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "the same time"? You can add an integer property.
 
Laythe Chamse
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, this is my code after solving the comparTo() implementation problem:

First Class:



Second Class:



the code wont compile and i get this Error:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

but when i do this:



The Code Compiles and Works!
So what do you think the problem is?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laythe:

Well, what does it mean when you try to add an object of type1 to a collection that holds objects of type2?

John.
 
Jaydeep Vaishnav
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the following code.

TreeSetX.add("1");
TreeSetX.add("2");
TreeSetX.add("3");

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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

* Create your own TreeSet object

* 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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not comparing on two fields, it's basically my ToStringComparator.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic