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