You'll notice the javadoc of TreeMap
states
The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
So if you don't provide a Comparator
, it will use natural ordering. What is natural ordering? The javadoc of the Comparable
interface states
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
So your key element in your TreeMap
must implement Comparable
. You are trying to use TreeSet
instances as keys in your TreeMap
. TreeSet
does not implement Comparable
.
You get a ClassCastException
when TreeMap
tries to cast the key to a Comparable
reference in order to use its compareTo
method.
To correct this issue, you should create a TreeMap
by providing your own custom Comparator
for comparing Set<Character>
instances.