Velocity – Looping through a TreeMap
Posted on November 26th, 2008 in Programming | Comments Off
I had trouble finding any real information on this to pass on to the developers here, so I had to document it myself, to help them move on from their PHP background to understand things other than simple multi-dimensional arrays.
So here, our TreeMap structure is….
TreeMap
tMap = new TreeMap ();
tMap.put(1, “London”);
tMap.put(2, “New York”);
tMap.put(3, “Paris”);
tMap.put(4, “Sydney”);
tMap.put(5, “Geneva”);
Looping through this in Velocity and getting the values OR the keys is easy enough to do, and can be found through searching on Google, but pulling out both is hard to find. Presuming we’re passed the above TreeMap to the Velocity layer as$cities
// loop through the keySet of our TreeMap and assign
// the key to $cityKey
#foreach ($cityKey in $cities.keySet())// using the current $cityKey, get the value assigned
// to that key and set it to the variable $city
#set ($city = $cities.get($cityKey))Key: $cityKey
City: $city#end
Very basic example, but hopefully it’s useful to someone!