wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparator Runtime Exception Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparator Runtime Exception Help" Watch "Comparator Runtime Exception Help" New topic
Author

Comparator Runtime Exception Help

Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
OK. I'm really missing the boat on this one:



And here is the runtime exception I get:

Exception in thread "main" java.lang.ClassCastException: test cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
at java.util.TreeSet.add(TreeSet.java:238)
at test.main(test.java:10)
Press any key to continue . . .

And the explanation provided with the question this code came from states that test needs to implement Comparable rather than Comparator to be able to sort its objects automatically. OK fine, if you say so.

I do see that the compare method has a questionable algorithm to compare thisObject to anotherObject, but I am completely misunderstanding why test should use Comparable vs Comparator, instead of just smartening up the compare method shown, and why is the JVM trying to cast test to java.lang.Comparable?

Can somebody please help me understand this?
Thanks again for your time.
g


G
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18716
    
  40

And the explanation provided with the question this code came from states that test needs to implement Comparable rather than Comparator to be able to sort its objects automatically. OK fine, if you say so.

I do see that the compare method has a questionable algorithm to compare thisObject to anotherObject, but I am completely misunderstanding why test should use Comparable vs Comparator, instead of just smartening up the compare method shown, and why is the JVM trying to cast test to java.lang.Comparable?


A comparator is an object that is used to compare two different objects. A comparable object is an object that doesn't need a comparator -- it has a compare method that allows it to be compared to another object.

When you construct a treeset, you are supposed to assign a comparator, so that the tree set can sort the items in the set. If no comparator is set, which is what you have done, then it will assume that the objects are comparable -- which is why it tries to cast them to comparable objects.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121

When you construct a treeset, you are supposed to assign a comparator, so that the tree set can sort the items in the set. If no comparator is set, which is what you have done, then it will assume that the objects are comparable -- which is why it tries to cast them to comparable objects.


Thank you for your response. Can you please tell me what the code should do in order to "set the comparator"? Are you saying the code should have included a "Collections.sort(s1,test)"?
thank you
g
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18716
    
  40

Originally posted by G Marshall:

Thank you for your response. Can you please tell me what the code should do in order to "set the comparator"? Are you saying the code should have included a "Collections.sort(s1,test)"?


Take a look at the available constructors for the treeset. You may contruct the treeset with a comparator. Or you may construnct the treeset from another treeset (sorted set) that has a comparator.

Henry
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Hi Henry,
In this code comparator is already implemented. What is the problem? Can you explain little more? I am not gettting.


SCJP-1.5<br />SCWCD-1.4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18716
    
  40

Originally posted by dolly shah:
Hi Henry,
In this code comparator is already implemented. What is the problem? Can you explain little more? I am not gettting.


Unfortunately, you never used it. Just because you have comparator objects in your set, doesn't mean that it will be used. You need to either (1) construct a tree set with a comparator, or (2) have the objects in your set be comparable. Having the objects in your set be comparators in not an option.

Henry
 
 
subject: Comparator Runtime Exception Help