Java HashMap can be iterated in FreeMarker by using a
<#list> tag as follows:
<#list hashMap?keys as key>
${key}=${hashMap[key]}<#list>
If you are using the above code with default FreeMarker Configuration then there would be some different behavior during iteration. For example if the HashMap is:
HashMap hashMap = new HashMap();
hashMap.put("1","one");hashMap.put("2","two");
By iterating on hashMap with default Configuration it will output following keys in result:
put
remove
entrySet
hashCode
productElement
clear
isEmpty
1
values
empty
underlying
productElements
copy
getClass
get
copy$default$1
equals
productPrefix
class
canEqual
keySet
size
2
containsKey
productArity
containsValue
productIterator
toString
putAll
This result shows that the iteration is done on all the available hashMap properties.
To get rid of this you have to set the FreeMarker Configuration as follows:
Configuration configuration = new Configuration();
BeansWrapper wrapper = (BeansWrapper) this.configuration.getObjectWrapper();
wrapper.setSimpleMapWrapper(true);
this.configuration.setObjectWrapper(wrapper);
Here the BeanWrapper's simpleMapWrapper property is set as 'true'. After setting this configuration the results over iterating on hashMap will give only the defined key-value pair.
rizzz86