wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TreeSet 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 "TreeSet" Watch "TreeSet" New topic
Author

TreeSet

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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

Joined: Feb 12, 2010
Posts: 266
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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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

Joined: Apr 29, 2008
Posts: 234
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

Joined: Feb 12, 2010
Posts: 266
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

Joined: Jan 29, 2010
Posts: 85
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.


Rajeev Trikha (SCJP 6)
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
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

Joined: Feb 02, 2010
Posts: 247
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.

SCJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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
 
GeeCON Prague 2014
 
subject: TreeSet