java與freemarker中map的遍曆

來源:互聯網
上載者:User

java中部分時間都是要的是list集合,偶爾會使用到map集合,但是經常會忘記map集合的如何遍曆,今天記錄下:

java中的遍曆

import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.Map.Entry;/** * map遍曆的三種辦法 * @author us * */public class test2{   public static void main(String[] args)    {           /**        * new一個map,然後新增內容        */       Map map=new HashMap();       for (int i = 0; i < 10; i++)        {            map.put(i+"",i+"");            System.out.println("添加"+i+"成功");        }       System.out.println("map大小"+map.size());              /**        * 1.把值放到一個集合力,然後便利集合        */       Collection c=map.values();       Iterator it= c.iterator();       for (; it.hasNext();)        {            System.out.println(it.next());        }              /**        * 2.把key放到一個集合裡,遍曆key值同時根據key得到值 (推薦)        */       Set set =map.keySet();       Iterator it=set.iterator();       while(it.hasNext()){           String s= (String) it.next();           System.out.println(map.get(s));       }              /**        * 3.把一個map對象放到放到entry裡,然後根據entry同時得到key和值        */       Set set =map.entrySet();       Iterator it=set.iterator();       while(it.hasNext()){           Map.Entry<String, String>  entry=(Entry<String, String>) it.next();           System.out.println(entry.getKey()+":"+entry.getValue());                  }           }}

freemarker中map集合的遍曆:

    <table width="300" border =1>        <thead>            <tr>                <th width="100">來源方式</th>                <th width="120">人數</th>            </tr>        </thead>        <tbody>            <#if clientSourceData?exists>                <#list clientSourceData?keys as key>                    <tr>                           <td>${key}</td>                           <td>${clientSourceData.get(key)}</td>                   </tr>                </#list>            </#if>            <tr>                   <td>合計:</td>                   <td>${totalNum}</td>           </tr>        </tbody>    </table>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.