Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator Runtime Exception Help

 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,
In this code comparator is already implemented. What is the problem? Can you explain little more? I am not gettting.
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic