How To Iterate HashMap in FreeMarker (FTL)

In this short article we will see how to iterate an HashMap in FreeMarker template. Consider below code which is normally used to iterate a List in FTL.

//Java
List<String> cityList = new ArrayList<String>();
cityList.add("Washington DC");
cityList.add("Delhi");
cityList.add("Berlin");
cityList.add("Paris");
cityList.add("Rome");
 
request.setAttribute("cityList", cityList);
 
//FTL template 
<#list cityList as city>
    <b> ${city} </b>
</#list>

Output:

Washington DC
Delhi
Berlin
Paris
Rome

Here in above code, we created a List object and passed it to FTL page. In FTL we used <#list> to iterate and print its values.

Problem Statement

So here’s the problem statement. I have an Hashmap which I want to iterate and display in my FTL (FreeMarker) template. Consider following code in Java.

So how to iterate this Hashmap in FTL and display its values?

//Java
Map<String, String> countryCapitalList = new HashMap<String, String>();
countryCapitalList.put("United States", "Washington DC");
countryCapitalList.put("India", "Delhi");
countryCapitalList.put("Germany", "Berlin");
countryCapitalList.put("France", "Paris");
countryCapitalList.put("Italy", "Rome");
 
request.setAttribute("capitalList", countryCapitalList);

Solution

Well, the above hashmap can be iterated in FTL using following code:

<#list capitalList?keys as key> 
    ${key} = ${capitalList[key]} 
</#list> 

Output:

United States = Washington DC
India = Delhi
Germany = Berlin
France = Paris
Italy = Rome

In above code, we used ?keys attribute to get the keySet of HashMap. This key set is iterated and corresponding value is fetched by user.get(key) method.

Note: One thing that’s worth noting here is that the sequence in which the values are printing may be different then the actual sequence in which values are added in hashmap. This is because, hashmap doesn’t preserve order or values. If you want to preserve the sequence of keys use LinkedHashMap.

Hope this helps.

Get our Articles via Email. Enter your email address.

You may also like...

7 Comments

  1. Nice one, exactly what I was looking for!
    Thanks for the post!

  2. WebDev says:

    How do i access the next element in a sequence in freemarker

    <#assign elements = [1,2,3,4]
    <#list elements as element>
       ${elements[element_index + 1]}
    </#list>
    

    I have tried this but doesnt work

  3. Batman says:

    How do I iterate over a hashmap<String,List> in freemarker? abc() takes as input only strings or arrays. Not lists & the hashmap which I am looking at contains Keys as String & Value as a list of strings.
    I want the output to be like :

    A= apple,mango,banana
    B = x,e,t,

    My code:

    abc().setTargeting(‘${key?js_string}’,’${key_value_list_map[key]}’);

  4. Tarvinder Singh says:

    Nice Article. But when I try this:

    ${key} = ${criteriaMap[key]}

    It throws exception:
    freemarker.core.InvalidReferenceException: Expression criteriaMap[key] is undefined.

    However, If i print key only using ${key} , the code works. I dont know why “criteriaMap[key]” is causing trouble.

    Thanks in advance

    PS: There is a Line in your article “corresponding value is fetched by user.get(key) method.”
    where is user in the article, its confusing.

  5. ddekany says:

    Staring from FreeMarker 2.3.25 you can do this: ${key} = ${value}

    • ddekany says:

      My last comment was mangled… so again, staring from FreeMarker 2.3.25 you can do this:

      ${key} = ${value}

    • ddekany says:

      I have no idea what the syntax of the comments is… a last attempt:

      ${key} = ${value}

Leave a Reply

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