Question

Is this possible? I don't think it is but I just want to make sure. This is what I was trying.

private Map enumToMap(Enum<?> e){
    EnumMap map = new EnumMap<e, String>(e.getClass()); // error can't resolve constructor for EnumMap
    for (History h : History.values()) {
        map.put(h, h.answer());
    }
    return map;
}
Was it helpful?

Solution

I suspect that History is an enum. Otherwise you will have a bound-type error.

If the History type is an enum, then the root of your problem is in the public EnumMap (Class< K > keyType) constructor, because as per javadoc, it:

Creates an empty enum map with the specified key type.

In your case, you're defining that the key type will be History and in the meantime you're passing an Enum.class to the constructor.

A valid definition would be:

new EnumMap<History, String>(History.class);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top