This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

TreeSet

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TreeSet arranges output automatically in SortedAscending order. However the below code gives me error. It did not error in case of HashSet and LinkedHashSet, however in case of TreeSet am getting error :



 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is a runtime error as follows :

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer incompatible with java.lang.String
at java.lang.String.compareTo(String.java:28)


My question is why cannnot we add String and Integer types to SortedSet.
 
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Although, it is very obvious what the error is... it is still a good idea to state what the error is. After all, you do want more people to help you right? Make it easy on them.

Henry
 
Henry Wong
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jose chiramal wrote:The error is a runtime error as follows :

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer incompatible with java.lang.String
at java.lang.String.compareTo(String.java:28)


My question is why cannnot we add String and Integer types to SortedSet.




Wow, that was quick... made my last post moot.... :-)


Basically, the compareable method for both String and Integer, can only be compared to String and Integer respectively. What is happening is that the TreeSet is trying to compare a String to Integer, or vice versa, and the compareTo() method is complaining.

If you want to have String and Integer in the same TreeSet, the best option is to write your own Comparator class for the TreeSet.

Henry
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason is:-

Integers and String objects are not mutually comparable with each other.

you can not compare String with Integer object.

TreeSet uses compareTo() method to compare objects. And we can not compare two objects which are not comparable to each other.

TreeSet needs to compare objects to sort the objects.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response . How could we know that its the compareTo() method and NOT the compare() method that TreeSet uses ?

Also how can I know what are the comparison methods(compareTo() and compare()) used by other collection classes like TreeMap etc..


Thanks a ton for your responses.
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The TreeSet implements neither Comparable nor Comparartor interface. For example, if you are using String values then it is the String class which has the implementation for Comparable.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jose chiramal wrote:Thanks for your response . How could we know that its the compareTo() method and NOT the compare() method that TreeSet uses ?

Also how can I know what are the comparison methods(compareTo() and compare()) used by other collection classes like TreeMap etc..


Thanks a ton for your responses.



TreeSet class implement the SortedSet interface. All elements which you are adding to the SortedSet need to implement Comparable interface.
You can get all the information from java docs. Just click TreeSet
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jose chiramal wrote:Thanks for your response . How could we know that its the compareTo() method and NOT the compare() method that TreeSet uses ?

Also how can I know what are the comparison methods(compareTo() and compare()) used by other collection classes like TreeMap etc..


Thanks a ton for your responses.



Jose,
There may be other ways of finding out, but I usually check the Java API Javadoc. In the case of TreeSet, it seems that it uses both compareTo() and compare() methods. Anyone, please tell me if I misread the javadoc.
 
Henry Wong
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jose chiramal wrote:How could we know that its the compareTo() method and NOT the compare() method that TreeSet uses ?

Also how can I know what are the comparison methods(compareTo() and compare()) used by other collection classes like TreeMap etc..



It depends on how the TreeSet is instantiated.

If the TreeSet is created with a Comparator (passed to the contructor), it will use that Comparator for all comparisons. If the TreeSet is not instantiated with a Comparator, it will assume that all the elements are Comparable.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!