Freemarker动态构造map,增加或改变元素

7 篇文章 0 订阅
1 篇文章 0 订阅
需求:遍历一个List,按照key值统计每个key值有多少个,将结果放到map中

由于freemarker不支持对map的直接操作,所以需要将对map的直接操作转化为重新构建新的map

<!-- 初始化map -->
<#assign creditDic = {}>
<!-- 遍历list -->
<#list report.creditTipss as item>
    <!-- 如果节点的mainType不为空且map中该键值对的值大于0,则对应加一,其他不变 -->
    <#if item.mainType && ((creditDic[item.mainType]!) > 0)>
        <#assign tempDic = {}>
        <#list creditDic?keys as keyItem>
            <#if item.mainType == keyItem>
                <!-- 对应加一 -->
                <#assign tempDic = tempDic + {item.mainType:(creditDic[item.mainType])+1}>
            <#else>
                <!-- 其他不变 -->
                <#assign tempDic = tempDic + {keyItem:(creditDic[keyItem])}>
            </#if>
        </#list>
        <!-- 需要将结果赋值给原来的map -->
        <#assign creditDic = {} + tempDic>
    <#else>
        <!-- 初始化对应键值为1 -->
        <#assign creditDic = creditDic + {item.mainType:1}>
    </#if>
</#list>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值