1
Set s = new HashSet();

boolean b[] = new boolean[5];
b[0] = s.add("a");
b[1] = s.add(new Integer(5));
b[2]= s.add("a");
b[3] = s.add(new Object());
b[4] = s.add("4");
for(int i=0;i<b.length;i++){
    System.out.println(b[i]);
}

This is giving me this output as expected : -

true
true
false
true
true

But when I am using Treeset

Set s = new TreeSet();

It is giving me this exception.

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at java.lang.Integer.compareTo(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at com.sunil.questions.TreeSetExample.main(TreeSetExample.java:15)

I want to know why it is showing me this behavior.

Theolodis
  • 4,977
  • 3
  • 34
  • 53
Java_Alert
  • 1,159
  • 6
  • 24
  • 50

4 Answers4

6
  • TreeSet is sorted, it cannot sort String and Integer together. Thats why you are getting this exception.

If you just add the same type of elements, then you will not get exception.

This is where the generics come into picture, if you want your collections to be type safe, then you can declare them as Set<String> set = new HashSet<String>() or Set<Integer> set = new TreeSet<Integer>();

Using this method, compiler itself will stop you from adding integers to Set<String> and likewise.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • @Java_Alert Why do you want different objects in one set anyways? – u6f6o Aug 23 '13 at 09:20
  • @UlfGitschthaler,maybe out of curiosity :) – Prasad Kharkar Aug 23 '13 at 09:25
  • @Prasad kharkar : Means we can put only same type of elements in TreeSet.Is it? – Java_Alert Aug 23 '13 at 09:26
  • 1
    @Java_Alert, As I said, `TreeSet` is a sorted collection. For a collection to be sorted, there must be some comparison thats why compareTo method gets called. If objects are to be compared, they must of same type. Hence TreeSet elements should have elements of same type – Prasad Kharkar Aug 23 '13 at 09:29
  • 3
    @Java_Alert It's not that simple. You can give the `TreeSet` a `Comparator` instance which handles whatever sorting you want, including your case with Integer and String. – Marko Topolnik Aug 23 '13 at 09:29
  • 1
    @Java_Alert yes you can: `public static void main(String[] args) { TreeSet objectSet = new TreeSet(new Comparator() { @Override public int compare(Object o1, Object o2) { // add your weird sorting here.... return 0; } }); objectSet.add(1l); objectSet.add(true); objectSet.add("no"); }` – u6f6o Aug 23 '13 at 09:30
  • @Java_Alert Although my previously posted code works, I'd really not recommend to mix different object types in one set, at least if there is a different way to achieve this – u6f6o Aug 23 '13 at 09:37
  • @UlfGitschthaler Yes sure I was only trying to know the reason. – Java_Alert Aug 23 '13 at 09:40
  • @UlfGitschthaler, there is a way to achieve this using `Comparator`. You can create your own sort sequence with the help of it. – Prasad Kharkar Aug 23 '13 at 09:45
3

The root of evil here is not using generics. You are instantiating a collection of unknown objects and expect Java to work correctly with them. Unfortunately, this time it can't - comparing String with Integer is not possible, at least not by default.

Use Set<String> s = new HashSet<String>(); and Set<String> s = new TreeSet<String>();. The compiler will then tell you that you can't add Integer to a collection, you will be forced to convert the Integer to a String and all will work well.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
2

Use generics to instantiate your collections. You have a HashSet/TreeSet which contain String as well as Integer. This is not recommended. TreeSet is sorted so it will not be able sort with String and Integer as no implicit conversion happens. Use Comparator interface if you want to sort String and Integer.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
Aashray
  • 2,753
  • 16
  • 22
1

As we know that TreeSet sort element while adding into it. Here u are adding string and integer at same time in TreeSet so u got error. U can use different tree set for string and integer and then can merge.