aspose file tools*
The moose likes Beginning Java and the fly likes why do i get TreeSet RuntimeException with my code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why do i get TreeSet RuntimeException with my code?" Watch "why do i get TreeSet RuntimeException with my code?" New topic
Author

why do i get TreeSet RuntimeException with my code?

Laythe Chamse
Greenhorn

Joined: Sep 06, 2009
Posts: 15
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

Joined: Jan 10, 2002
Posts: 61216
    
  66

"Laythe Laythe", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Laythe Chamse
Greenhorn

Joined: Sep 06, 2009
Posts: 15
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Mar 24, 2008
Posts: 3829

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.

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Laythe Chamse
Greenhorn

Joined: Sep 06, 2009
Posts: 15
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

Joined: Sep 29, 2008
Posts: 12617

What do you mean by "the same time"? You can add an integer property.
Laythe Chamse
Greenhorn

Joined: Sep 06, 2009
Posts: 15
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

Joined: Mar 09, 2009
Posts: 600
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

Joined: Sep 08, 2009
Posts: 16
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

Joined: Sep 06, 2009
Posts: 15
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

Joined: Sep 06, 2009
Posts: 15
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
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
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Oct 27, 2005
Posts: 19693
    
  20

That's not comparing on two fields, it's basically my ToStringComparator.
 
Consider Paul's rocket mass heater.
 
subject: why do i get TreeSet RuntimeException with my code?