在<logic:iterate> </logic:iterate>中按插入顺序迭代HashMap

时间:2012-03-14 17:04:37

标签: java struts hashmap loops

我有这样的HasMap对象:

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

fileCounter.put("Total Files","15");
fileCounter.put("Total Success Files","10");
fileCounter.put("Total Failed Files","2");
fileCounter.put("Total In Process Files","4");
fileCounter.put("Total Records","100");
fileCounter.put("Total Success Records","80");
fileCounter.put("Total Failed Records","10");
fileCounter.put("Total In Process Records","10");

我的迭代器逻辑是:

<logic:iterate id="mapEntry" name="fileCounter">
  <tr>
    <td><bean:write name="mapEntry" property="key"></td>
    <td><bean:write name="mapEntry" property="value"></td>
  </tr>
</logic:iterate>

问题:

当我执行迭代时,映射值和键被洗牌,因此所有的迭代都不会像java代码中那样打印出来。

任何人都可以帮助我,如何按照给定的顺序打印这张地图。

我想要这样的输出:

Total Files         15
Total Success Files     10
Total Failed Files      2
Total In Process Files  4
Total Records       10
Total Success Records   80
Total Failed Records    10
Total In Process Records    10

1 个答案:

答案 0 :(得分:7)

使用LinkedHashMap代替常规HashMap。它按照插入项目的顺序进行迭代。