I've been trying to define an EnumMap in Spring using . I tried the following variations
<util:map map-class="java.util.EnumMap" key-type="xyz.EnumType">
<entry key="SOME_ENUM_TYPE">
<ref bean="someBean"/>
</entry>
</util:map>
I get the following error
Error creating bean with name 'util:map#1c599b0e': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.EnumMap]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.util.EnumMap.<init>()
The following definition is what I tried initially
<util:map map-class="java.util.EnumMap">
<entry key="SOME_ENUM_TYPE">
<ref bean="someBean"/>
</entry>
</util:map>
and this gave me some error of not being able to assign the enumtype to String.
There are examples on the site to using a generic map, but I am trying to see whether I can use an EnumMap, since it's considered the most optimal for Enums. The answer might be very obvious, so my apologies if the question is stupid. This is probably due to my limited knowledge of Spring. Thanks
EnumMap is a specialized implementation of the Map interface for the enumeration types. EnumSet is a specialized implementation of the Set interface for the enumeration types. EnumMap is internally represented as an array. EnumSet is internally represented as a BitVector.
Using Enum as key makes it possible to do some extra performance optimization, like a quicker hash computation since all possible keys are known in advance.
An EnumMap uses the restriction that all keys of a map will be from the same enum to gain performance benefits: Enum maps are represented internally as arrays. This representation is extremely compact and efficient. In this case, the keys and values are stored in separate arrays and the values are ordinal-ordered.
I guess you cannot initialize EnumMap
with <util:map>
. However, EnumMap
has a constructor that takes an existing Map
, you can try to use it:
<bean class = "java.util.EnumMap">
<constructor-arg>
<util:map key-type="xyz.EnumType">
<entry key="SOME_ENUM_TYPE"><ref bean="someBean"/></entry>
</util:map>
</constructor-arg>
</bean>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With