在jsp中迭代嵌套的哈希映射

时间:2014-09-17 19:42:23

标签: java jsp hashmap nested jstl

我写了一个像这样的嵌套hashmap ......

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

    choice_map.put("4","<?php ?>");
    choice_map.put("2","<? ?>");
    choice_map.put("1","<? >");
    choice_map.put("3","< ?>");

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

    que_id_map.put("1",null);

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

    que_type_map.put("Objective",null);

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

    que_map.put("What is Short Tag in PHP?",null);

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


    main_choice_map.put("question",que_map);
    main_choice_map.put("question_type_id",que_id_map);
    main_choice_map.put("question_type",que_type_map);
    main_choice_map.put("choices",choice_map);

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

   questions_with_choices.put("2",main_choice_map);

上述hashmap(questions_with_choices)的输出如下:

{2 = {question_type_id = {1 = null},question_type = {Objective = null},choices = {3 =&lt; ?&gt;,2 =&lt;? ?&gt;,1 =&lt;? &gt;,4 =&lt;?php?&gt;},question = {什么是PHP中的短标记?= null}}}

如何在jsp或jstl中迭代这个hashmap?

请帮帮我..

1 个答案:

答案 0 :(得分:0)

<%
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
%>
    <h1><%=pairs.getKey()%> = <%=pairs.getValue()%></h1>
<%
    it.remove(); // avoids a ConcurrentModificationException
}
%>