Search code examples
javaenumsenum-map

Store primitive values in an EnumMap


Why can't I create an EnumMap like this:

EnumMap<FunkyTrolls, int> amountMap;

I want to count and save the number of trolls of each type. What is a good way of doing this?


Solution

  • Just use Integer. Generics only work on objects, not on primitive types, but Java now has auto-boxing and -unboxing.

    This should work:

    Map<FunkyTrolls, Integer> amountMap = new EnumMap<FunkyTrolls, Integer>();
    amountMap.put(FunkyTrolls.VERY_FUNKY_TROLL, 100);