Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Comparator Runtime Exception Help

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic