freemarker list集合去重,实现hashset

在freemarker中没有提供去重的方法,虽然有提供定义hash的方法,如:<#assign myHash = { "name": "mouse", "price": 50 }>,但是不能够动态构建hash。

通常我们会在java代码中进行处理,比如使用set集合、map对象处理。

但是如果一定要在freemarker文件中实现还是可以做到的,如下代码所示:

introspectedTable.allColumns为java传递过来的ArrayList集合。下面进行去重处理


<#list introspectedTable.allColumns as allColumns>
        <#list introspectedTable.allColumns as allColumns2>
                <#if allColumns.fullyQualifiedJavaType.fullyQualifiedNameWithoutTypeParameters ==allColumns2.fullyQualifiedJavaType.fullyQualifiedNameWithoutTypeParameters>
                    <#if allColumns_index==allColumns2_index>
                        import ${allColumns.fullyQualifiedJavaType.fullyQualifiedNameWithoutTypeParameters};
                    <#else>
                        <#break >
                    </#if>
                </#if>
        </#list>
</#list>



执行结果为:

import java.lang.Integer;
import java.lang.String;



最后显示的就是去重之后的内容,原理:使用值和_index两个一起来实现的。
原文地址:https://www.cnblogs.com/wulm/p/11067426.html