I am storing an arrayList as my keys in a TreeMap but I am getting this exception
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Comparable
I copy the content of array to ArrayList and try to store that arrayList as my keys in the Map My Code is :
TreeMap< ArrayList<Integer> , Integer > bandsMap = new TreeMap< ArrayList<Integer> , Integer >();
ArrayList< Integer > erfcn = new ArrayList< Integer >();
for (int index = 0; index < frequencies.length; index++)
erfcn.add(frequencies[index]);
bandsMap.put( erfcn , band_number);
for (Integer value : bandsMap.values()) {
System.out.println("Value = " + value + "\n");
}
Any Idea ? Thanks
java.util.ArrayList cannot be cast to java.lang.Comparable.Listdoesn't extendsComparable, then there are not defined comparing operation throughcompareTo(T o)method on it. You can't use as map key.TreeMapcompares keys before adding them. How do you think it's going to compare two instances ofArrayList? What is the criteria that determines that a list is smaller/bigger/equal to another?List<Integer>for the keys? Can you think of a possible workaround?