Java – how to sort the values of the HashMap freemaker template

I have this HashMap in Java:

HashMap<String,String> map = new HashMap<String,String>();

     map.put("k1","3");
     map.put("k2","4");
     map.put("k3","2");
     map.put("k4","6");
     map.put("k5","1");
     map.put("k6","5");

I print using freemaker template in this mode:

<#list map?values as v>
${v} - 
</#list>

But it prints in this order:

2 - 6 - 1 - 5 - 3 - 4

I want to print in this order:

1 - 2 - 3 - 4 - 5  -6

How do I use freemaker templates to sort values?

Solution

Try this:

<#list map?values?sort as v>
    ${v} - 
</#list>

Note the use of built - in types for value sequences

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>