Creating a HashMap in Velocity Template

I just cant find an example of how to create a HashMap in velocity. I want to create an empty HashMap and put values in it for later retrieval. Any help on this would be grateful.

Answer

That’s pretty easy:

#set($myMap = {})
#set($map.foo = 'bar')
#set($map[1234] = 3456)
Map is: $map

This displays:

Map is: {foo=foo, 1234=1234}

The map is actually a java.util.LinkedHashMap.

Leave a Reply

Your email address will not be published. Required fields are marked *