java map在JSTL EL中的小應用-- 遍歷Map


准備數據

 1  /**    結構示意圖:
 2   類型:  List集合    map對象    LIst集合   Person類對象 String name ; int age
 3 
 4 
 5       mList    ->    map()    ->pList     ->Person p1 坤哥 24
 6                                           ->Person p2 鯤哥 104
 7                              ->pList2
 8                                           ->Person ps1 王小三 24
 9                                           ->Person ps2 王小二 24
10                ->    map2()
11                              ->pList3
12                                           ->Person pr1 張咪咪  19
13                                           ->Person pr2 趙咪咪  21
14                             - >pList4
15                                           ->Person pd1 謝廣坤  54
16                                           ->Person pd2 趙四    56  (可能是吧)
17 */

Map所需包 (只限JSTL中)

java.util.HashMap

 

java.util.Map

常 見 Map 指 令 清 單

1. 創建map

Map<String,List<Person>> map = new HashMap<String,List<Person>>();

 

List<Map<String,List<Person>>> mList = new ArrayList<Map<String,List<Person>>>();

其實結構確實不難的。(O - O)```

2. map添加數據

map.put("謝廣坤",54)

3. 獲取key

 map.keySet()   和   map.entrySet() 

本處用keySet()。因為我菜,用entrySet()遍歷的數據不太正常,哪天解決了再寫吧。

4. 獲取value

 map.values()   注意 ‘ s ’

我 的 主 要 代 碼

1. 數據准備

//以謝廣坤為例
List<Person> pList4 = new ArrayList<Person>();
Person pd1 = new Person("謝廣坤",54);
Person pd2 = new Person("趙四",56);
pList4.add(pd1);pList4.add(pd2);
//。。。
Map<String,List<Person>> map2 = new HashMap<String,List<Person>>();
map2.put("pList4",pList4);
//。。。
List<Map<String,List<Person>>> mList = new ArrayList<Map<String,List<Person>>>();
mList.add(map2);

 

2. 輸出所有數據

<c:forEach items="${mList}" var="m" varStatus="id">
        <h2>第${id.count}個map</h2>
    <c:forEach items="${m.keySet()}" var="k">
    <h3>List的名字是:<c:out value="${k}"></c:out></h3>
        <c:forEach items="${m.values()}" var="l">
            <c:forEach items="${l}" var="p">
                <table border="1px dotted blue">
                    <tr>
                        <th>姓名</th>
                        <th>年齡</th>
                    </tr>
                    <tr>
                        <td>${p.name}</td>
                        <td>${p.age}</td>
                    </tr>
                </table>
                <br>
            </c:forEach>
        </c:forEach>
    </c:forEach>
</c:forEach>

 

3. 輸出年齡大於50的鄉村愛情人物

<c:forEach items="${mList}" var="m">
    <c:forEach items="${m.values()}" var="l">
        <c:forEach items="${l}" var="p">
            <c:if test="${p.age>50}">
                <table border="2px dotted blue">
                <tr>
                    <th>姓名</th>
                    <th>年齡</th>
                </tr>
                <tr>
                    <td>${p.name}</td>
                    <td>${p.age}</td>
                </tr>
            </table>
            </c:if>
        </c:forEach>
    </c:forEach>
</c:forEach>

結 果 樣 子

大概就是這個樣子

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM