• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeSet

 
jose chiramal
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21417
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
Marshal
Pie
Posts: 21417
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
Brij Garg
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.
 
Rajeev Trikha
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
 
Larry Chung
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
Marshal
Pie
Posts: 21417
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic