有一个将错误信息统计并且发送mail的程序,在最终获取了错误信息并发送之后,用户发现每次收到的mail中,错误信息的显示是无序的。
检查了一下,是因为用HashMap存储了错误信息,直接输出,造成了无序状态。
原始代码如下:
Map errTaskMap = new HashMap<Long, String>();
...
...
for (Long keyVal : (Collection<Long>)errTaskMap.keySet()) {
try {
String s = taskMap.get(keyVal);
...
...
} catch (Exception e) {
;
}
} // end loop.
修改如下:
ArrayList errList = new ArrayList(errTaskMap.keySet());
Collections.sort(errList);
Iterator it = errList.iterator();
while (it.hasNext()) {
try {
String s = taskMap.get(it.next());
...
...
} catch (Exception e) {
;
}
} // end loop.