The structure is like this:
public interface ItemList{ }
public enum ItemList1 implements ItemList {
Apple,
Orange;
}
public enum ItemList2 implements ItemList {
Banana,
Grapes;
}
and 5 more such enums
The requirement is to use these enums as Keys in a Map, and in those Maps I have put key as:
public Class SomeClass {
private Map<ItemList, OtherObject> objectList;
//other code
}
The ItemList which goes into the map is decided on runtime. And I need to use a sorted Map like TreeMap for other operations. So, the TreeMap is unable to compare the key enums obviously because I have declared them as ItemList supertype.
So, I searched other questions and did something like this so that enums could use their compareTo method:
public interface ItemList<SelfType extends ItemList<SelfType>> extends Comparable<SelfType>{ }
public enum ItemList1 implements ItemList<SelfType> {
//enum values
}
But this doesn't solve the problem. I am still getting the same "ClassCastException" when I tried to retrieve a TreeMap which had my enums as Keys.
Please suggest if I am doing anything wrongly here, or what can be an other way to solve this purpose?
EDIT: Link to the solution which I followed, but it's not working: How to implement an interface with an enum, where the interface extends Comparable?
EDIT 2 Problem identified. Sorry guys. My map was getting populated with different types of enums as keys, when all the keys should belong to same type for sorting to work.