I have created a TreeMap which is having arguments of type Set as both key value and the sets are also TreeSet so that the key and value pairs are always corresponding to each other.But I am getting an error as :
Exception in thread "main" java.lang.ClassCastException: java.util.TreeSet cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1290)
at java.util.TreeMap.put(TreeMap.java:538)
at hello.Sam.main(Sam.java:49)`
Here is The code below:
package hello;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class Sam {
public static void main(String args[])
{
Set<String> set=new TreeSet<String>();
Set<String> set2=new TreeSet<String>();
List<String> list=new ArrayList<String>();
list.add("hello1");
list.add("bye1");
list.add("bye2");
list.add("bye3");
list.add("bye4");
list.add("bye5");
list.add("bye6");
Map<Set<String>,Set<String>> map=new TreeMap<Set<String>,Set<String>>();
set.add("set11");
set.add("set12");
set.add("set13");
set.add("set14");
set.add("set15");
set.add("set16");
set.add("set17");
set2.add("set21");
set2.add("set21");
set2.add("set21");
set2.add("set21");
set2.add("set21");
set2.add("set21");
set2.add("set21");
map.put(set,set2);
System.out.println(map);
}
}
But When I use Key Or value as String Like (String,Set) or (Set,String) it works fine.
Can I not have both the key and value as collection? or If yes how to use them??